Results 1 to 5 of 5
  1. #1
    javaplus is offline Member
    Join Date
    Nov 2007
    Posts
    97
    Rep Power
    0

    Default creating array at runtime

    Hi,

    I want to create an array at run time. I am not sure about its size when coding. I take an input from user through console and then want to create the array of that size.

    Java Code:
    int[] array = new int [5];
    In the example above, I mentioned the size which is fine. I just want to know if size can be given later?

    Is this possible? How?

    Thanks.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    if size can be given later
    No. You have to instantiate an array with a size. You can change the size later by making a new array of the same type at a different size and copying the elements of the old array into it - actually assigning the new elements the value of the old elements. The System.arraycopy method is useful for this.

  3. #3
    hiranya is offline Member
    Join Date
    Jun 2007
    Location
    Colombo, Sri Lanka
    Posts
    32
    Rep Power
    0

    Default

    You could do something like this.

    Java Code:
    int[] array = new int[size];
    Here size is an integer variable with some value. If the value of 'size' is determined at the runtime then your array size will be determined at runtime.

    Regards,
    Hiranya

  4. #4
    javaplus is offline Member
    Join Date
    Nov 2007
    Posts
    97
    Rep Power
    0

    Default

    Thanks. Its helps.

  5. #5
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    I just want to know if size can be given later?
    You can re/instantiate the array at any time.
    Java Code:
    class Pseudo {
        int[] sizes;                  // declaration
        String[] ids = new String[0]; // declaration and instantiation;
    
        void someMethod() {
            // Instantiate the sizes array declared as a member variable.
            // Java assigns each element the default value of zero.
            sizes = new int[16];
            // Instantiate the ids array declared and insantiated as a
            // (zero-length array) member variable.
            // Each element of this array is null.
            ids = new String[4];
        }
    }

Similar Threads

  1. Creating an array of nonprimitive objects
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 08:46 PM
  2. Need help with creating array of type object
    By riz618 in forum New To Java
    Replies: 3
    Last Post: 01-29-2008, 06:14 AM
  3. Creating Array of LinkedList
    By sasikumardr in forum New To Java
    Replies: 1
    Last Post: 12-11-2007, 10:25 AM
  4. How to initialize array at runtime
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-09-2007, 03:47 PM
  5. Initialize array at runtime
    By javaplus in forum Java Tip
    Replies: 2
    Last Post: 11-09-2007, 11:44 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
  •