Click to See Complete Forum and Search --> : Resizing Arrays
US101
Aug 1st, 2001, 06:41 AM
How can you resize arrays?
int Old = 10;
int new = 20;
byte Array[] = new byte[Old];
Resize it to New??
Dillinger4
Aug 1st, 2001, 01:31 PM
I think that's about the only thing that you can't do. You can use the java.util.Arrays class to do binary searches, you can do sorts, fills. You can use the arrayCopy() method from the java.lang.system class to copy array elements into a new array.
You can clone arrays such as..........
String[] strArray = new String[] {"A","B","C"};
String[] clone = (String[]) strArray.clone();
boolean b = Arrays.equals(strArray,clone) // they are equal
but i dont think you can resize an array such as in Visual Basic
using ReDim or ReDim preserve. If you want an expandable array of objects why dont you usew a Vector? java.util.Vector; then you can enumerate through it. Memory is allocate dynamicaly using a Vector so it only use enough memory as needed. I think when shrinking a Vector you have to can the trimToSize() method though.
Dillinger4
Sep 23rd, 2001, 10:59 AM
If you want you can convert arrays to Lists or Sets and Lists and Sets to arrays. This can be acheived by using the asList() method
defined in the java.util.Arrays class and the toArray() method defined in the Collection interface . Heres an example.
import java.util.*;
public class CollectionTest{
public static void main(String[] args){
String[] greeting = {"Hello", "my", "name", "is", "brandon"};
// convert array to list.........
List l = new LinkedList(Arrays.asList(greeting));
// itenerate through the list
ListIterator literator = l.listIterator();
while(literator.hasNext()){
System.out.println(" Output from l " + literator.next());
}
// list back to String array........
String[] uniquearray = (String[]) l.toArray(new String[0]);
for(int i = 0; i < uniquearray.length; ++i){
System.out.println(" Output from uniquearray " + uniquearray[i]);
}
}
}
Dillinger4
Nov 11th, 2001, 02:00 PM
Also the java.util.Vector and java.util.HashTable classes and declared synchronized which means that the integrity of their data is not jeopardized by concurrent access. So ArrayList might give you better performance over the other two unless synchronization is important.
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.