Growing an Array in Java Suppose you have an array of some type that is full, and you want to grow it. Code: 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: 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: a = (Employee[]) arrayGrow(a); // throws ClassCastException. Use reflection to make a new array of the same type: Code: 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: 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: 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!
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.
This should work for you. Code: a = [1,2,5,6,6,3,3,2,9,6,8]; b = []; for(i=0;i<a.length;i++) { in_a = false; for(j=0;j<a.length;j++) { if(a[j]==a[i]) in_a = true; } if(!in_a) b.push(a[i]); }
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.... -macknonalds-