Results 1 to 4 of 4

Thread: Resizing Arrays

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 2001
    Location
    Los Angeles
    Posts
    45

    Resizing Arrays

    How can you resize arrays?

    int Old = 10;
    int new = 20;

    byte Array[] = new byte[Old];

    Resize it to New??

  2. #2
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    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.

  3. #3
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    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.

    Code:
    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]); 
      }  
     } 
    }
    Last edited by Dilenger4; Nov 11th, 2001 at 02:54 PM.

  4. #4
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width