This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Java in General and the fly likes resize two arrays ... guide me ... Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "resize two arrays ... guide me ..." Watch "resize two arrays ... guide me ..." New topic
Author

resize two arrays ... guide me ...

Manisekar Chinnasami
Greenhorn

Joined: Apr 07, 2007
Posts: 24
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 ...


Manisekar
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24061
    
  13

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.


[Jess in Action][AskingGoodQuestions]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

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.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24061
    
  13

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

Joined: Apr 07, 2007
Posts: 24
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

Joined: Sep 28, 2004
Posts: 16695
    
  19

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 ]

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Manisekar Chinnasami
Greenhorn

Joined: Apr 07, 2007
Posts: 24
yup .. you are correct ...
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: resize two arrays ... guide me ...
 
Similar Threads
doubt in basics of arrays
Adding Arrays
Could you please help me with this ?!!!
logic iterate problem for multiple input parameters
Two dimensional data structures?