Java Arrays

S k's Avatar author of Java Arrays
This is an article on Java Arrays in Java.

Growing an Array in Java

Suppose you have an array of some type that is full, and you want to grow it.
Code: JAVA
Employee[] a = new Employee[100];
// array is full
int newLength = a.length * 11 / 10 + 10;
Employee[] newArray = new Employee[newLength];
System.arraycopy(a, 0, newArray, 0, a.length);
a = newArray;
That gets boring really quickly. Let's try to make it generic.
Code: JAVA
static Object[] arrayGrow(Object[] a) // not usefull
    int newLength = a.length * 11 / 10 + 10;
    Object[] newArray = new Object[newLength];
    System.arraycopy(a, 0, newArray, 0, a.length);
    return newArray;
Problem: Return type is Object[], not Employee [] .
Code: JAVA
a = (Employee[]) arrayGrow(a); // throws ClassCastException.
Use reflection to make a new array of the same type:
Code: JAVA
static Object arrayGrow(Object a) // useful
    Class cl = a.getClass();
    if (!cl.isArray()) return null;
    int length = Array.getLength(a);
    int newLength = length * 11 / 10 + 10;
    Class componentType = a.getClass().getComponentType();
    Object newArray = Array.newlnstance(componentType, newLength);
    System.arraycopy(a, 0, newArray, 0, length);
    return newArray;

Typical usage:

Code: JAVA
Employee[] a = new Employee[100]; . . . . .// array is full
a = (Employee[]) arrayGrow(a);
This arrayGrow method can be used to grow arrays of any type, not just arrays of objects.
Code: JAVA
int[] ia = ( 1, 2, 3, 4 };
ia = (int[])arrayGrow(a);
Note: The parameter of arrayGrow is declared to be of type object, not an array of objects (Object [] ). The integer array type int [] can be converted to an object, but not to an array of objects!
shoyeb4me's Avatar, Join Date: Oct 2006
Newbie Member
I am a novish in Java Wolrd. I m trying to learn it. Anyway I am getting one problem while I am trying to solve one

problem. The problem is

First you have create an array A of unsorted random integers and will have to repoduce an array B which contains

all integres from A without repetitions.

For example, given array A = [1,2,5,6,6,3,3,2,9,6,8], the program will produce array B = [1,2,5,6,3,9,8] where no

repetitions are allowed .

If anybody please help me by providing code I will be very happy and greatful.It will help me to learn.

Thank u all.
pradeep's Avatar, Join Date: Apr 2005
Team Leader
This should work for you.

Code: Java
a = [1,2,5,6,6,3,3,2,9,6,8];
 b = [];
    in_a = false;
         in_a = true;
macknonalds's Avatar, Join Date: Jan 2007
Light Poster
hello guyz, can anybody help me about this problem:

"Create a java program, that will compute the union, intersection, and difference of a given sets... These given sets must be coming from a user. Example, they want 3 sets or 4 or 5 sets (it must be at least 2 sets) and these set will be named as set 1, set 2 ,set 3 and so on, depending on how many sets the users declared. After he/she declared the number of sets, he will then input the value of the each sets and once the user typed the word "exit", it will stop inputing values with in the set and then proceed to another set where the user will again input some values, until the user will satisfy the values of each set....And then these sets will be manipulated and will show/display the intersection, union, and difference of the sets, thus, the user will choose which sets will be manipulated... HINT: Use two-dimensional array(s) for the sets! (storage)"

I've been on the process of solution but still I'll always stocked-up od decaring the number of sets and the storing of the values of the specified number of sets....

thanks for the help and for the ideas... I really appreciate it much....
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
macknonalds, try creating a new thread in the forum with an appropriate title for the query.
macknonalds's Avatar, Join Date: Jan 2007
Light Poster
sorry for these....

thanks, shabbir....