Go4Expert (http://www.go4expert.com/)
-   Java (http://www.go4expert.com/articles/java-tutorials/)
-   -   Java Arrays (http://www.go4expert.com/articles/java-arrays-t1598/)

S k 12Oct2006 10:17

Java Arrays

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 15Oct2006 19:33

please help me urgent
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 16Oct2006 18:39

Re: Java Arrays
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 7Jan2007 11:22

Re: Java Arrays
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 7Jan2007 12:42

Re: Java Arrays
macknonalds, try creating a new thread in the forum with an appropriate title for the query.

macknonalds 9Jan2007 17:04

Re: Java Arrays
sorry for these....

thanks, shabbir....

All times are GMT +5.5. The time now is 00:34.