• Post Reply Bookmark Topic Watch Topic
  • New Topic

resize two arrays ... guide me ...  RSS feed

 
Manisekar Chinnasami
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello friends ,

In my program, i am about to resize tow arrays' size.

i could do only for one using, java.lang.reflect.Array;

i dont know how to resize two arrays with different values ie. one array should be incremented by one(that i ve done). the other should to decremented by the calculated values (might be 2 or 1).

someone guide me ...
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't resize arrays; once created, they have a fixed size. You have to allocate a new array and copy the contents from the old one (perhaps using System.arraycopy().) That's basically what ArrayList does; it's a wrapper around an array that can transparently expand the array as needed by reallocating and copying.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, since Java 1.6, you should rather use the java.util.Arrays.copyOf methods. This does most of the the hard work for you - creating the new array, copying the data, and for object arrays casting it to the type of array passed to it.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rob Prime:
Actually, since Java 1.6, you should rather use the java.util.Arrays.copyOf methods. This does most of the the hard work for you - creating the new array, copying the data, and for object arrays casting it to the type of array passed to it.


Cool -- thanks!
 
Manisekar Chinnasami
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i can resize the array ...

COPYING ANY ARRAY IS THE OLD BUSINESS ...

here is a sample program for resizeing an array ...

import java.lang.reflect.Array;

public class ArrayResize {

public static void main (String arg[]) {

int i[] = new int[5];
for(int j=0;j<5;j++)
{
System.out.println("The i array length is " + i.length);
i = (int[])ArrayUtils.expand(i);
}
}
public static Object expand(Object a) {
Class cl = a.getClass();
if (!cl.isArray()) return null;
int length = Array.getLength(a);
int newLength = length +1; // the size of the array is incremented by 1
Class componentType = a.getClass().getComponentType();
Object newArray = Array.newInstance(componentType, newLength);
System.arraycopy(a, 0, newArray, 0, length);
return newArray;
}
}

but for resizing two arrays with different values , i feel struck ...
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i can resize the array ...

COPYING ANY ARRAY IS THE OLD BUSINESS ...


Take a look at your code again...



You are creating an new array, and then copying the elements from the old array to the new array. You are not directly using the new operator, or using a loop (but the array copy method is), but that doesn't mean you are resizing an array.

If an array can be resized, then you should be able to write the method to return the same array object (that is resized).

Henry
[ November 02, 2007: Message edited by: Henry Wong ]
 
Manisekar Chinnasami
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yup .. you are correct ...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!