Results 1 to 8 of 8
  1. #1
    ddatta8 is offline Member
    Join Date
    Dec 2008
    Posts
    27
    Rep Power
    0

    Default [SOLVED] dynamically sized arrays

    I want to have arrays in my Java program whose size can be adjusted dynamically during execution. How do I do that in Java? Please help.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Look up ArrayLists

  3. #3
    MK12's Avatar
    MK12 is offline Senior Member
    Join Date
    Jan 2009
    Posts
    185
    Rep Power
    6

    Default

    Yes, ArrayLists are what you are looking for. The only catch is that they must contain Objects. That means no primitives, e.i. no ints, longs, doubles, floats, bytes, chars, shorts, etc. If you wanted it to contain numbers, you will have to use the Object Wrappers for the primitives, i.e. Integer class, Double class, etc. Strings are Objects also if you were planning on putting those in the array. Unless you were planning on having different types of Objects in the ArrayList, i.e. index 0 is an Integer, object 1 is a String, object 2 a BigDecimal, etc., you should use generics, here is an example assuming that you are using Integers:
    Java Code:
    import java.util.ArrayList;
    class ArrayListExample {
         public void doExample() {
              /* ArrayList size will automatically increase when you
                  try to add another object and it is full. The argument
                  int the ArrayList constructor used here is the initial
                  size of the ArrayList. I believe if you just do () in-
                  stead of (size), it will assign a default capacity of 10
               */
              ArrayList<Integer> aList = new ArrayList<Integer>(4);
              for(int i = 0; i < 18; i++) {
                   aList.add(new Integer(i));
              }
              aList.trimToSize(); // this deletes any empty spaces in the list, 
                        // but it doesnt do anything in this situation because our
                        // list started out at size 4 and then each new Integer in-
                        // creased the size by 1
              int[] anArray = new int[aList.size()]; // put it into int array
              for(int i = 0; i < anArray.length; i++) {
                   anArray[i] = aList.get(i).intValue();
              }
         }
    }
    Hope this Helped
    -MK12
    Tell me if you want a cool Java logo avatar like mine and I'll make you one.

  4. #4
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    St. Claus exists ) ddatta8 , you are lucky today ;)

    MK12, I still recognize the generous person in the forum )))

  5. #5
    ddatta8 is offline Member
    Join Date
    Dec 2008
    Posts
    27
    Rep Power
    0

    Default

    Thanks a lot. It is very helpful.

  6. #6
    MK12's Avatar
    MK12 is offline Senior Member
    Join Date
    Jan 2009
    Posts
    185
    Rep Power
    6

    Default

    Webuser: :). ddata8: please go to the top of this page and click Thread Tools and then in the drop-down menu, click Mark This Thread As Solved, so no one wastes time going to this thread thinking that it hasn't been solved yet.
    Tell me if you want a cool Java logo avatar like mine and I'll make you one.

  7. #7
    derekitishere is offline Member
    Join Date
    Apr 2009
    Posts
    3
    Rep Power
    0

    Default I want to say hi

    My name is Derek and
    I'm glad to join to this forum.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Welcome. Now if you want to introduce yourself, there's a forum for this where you should post this. You shoudn't hijack someone else's thread.

Similar Threads

  1. array passing dynamically
    By jazz2k8 in forum Advanced Java
    Replies: 2
    Last Post: 10-16-2008, 10:29 PM
  2. Dynamically changing the display
    By abhiN in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 01-22-2008, 11:19 PM
  3. passing data dynamically
    By abhiN in forum Advanced Java
    Replies: 1
    Last Post: 01-22-2008, 09:43 AM

Posting Permissions

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