Results 1 to 6 of 6
  1. #1
    artemff is offline Member
    Join Date
    Dec 2009
    Posts
    41
    Rep Power
    0

    Question How increase dynamic array?

    How increase dynamic array to save all it's elements?

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    If its a dynamic array, then it increases automatically, hence the name 'Dynamic' array.

  3. #3
    artemff is offline Member
    Join Date
    Dec 2009
    Posts
    41
    Rep Power
    0

    Default

    quad64bit, dynamic array, for example such:
    Java Code:
    int a[];

  4. #4
    pbrockway2 is online now Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,569
    Rep Power
    12

    Default

    Quote Originally Posted by artemff View Post
    quad64bit, dynamic array, for example such:
    Java Code:
    int a[];

    In Java the length of array types do not change. See the "Arrays" section in Sun's Tutorial.

    If you want a simple data structure that will hold an indeterminate number of elements use a List instead.

  5. #5
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    Java Code:
    int a[];
    Is not a dynamic array. It is just an array. There is no way to expand an array after the fact, you must either copy the contents to a bigger array or pick a different data structure such as ArrayList.

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

    Default

    Java Code:
    import java.util.Arrays;
    
    public class Test {
        public static void main(String[] args) {
            int[] values = { 5, 9, 3, 6 };
            System.out.println(Arrays.toString(values));
            // Add 7 to the array.
            int[] temp = new int[values.length+1];
            System.arraycopy(values, 0, temp, 0, values.length);
            temp[values.length] = 7;
            values = temp;
            System.out.println(Arrays.toString(values));
        }
    }

Similar Threads

  1. increase pixel size
    By rosh72851 in forum New To Java
    Replies: 9
    Last Post: 07-31-2012, 08:30 PM
  2. Increase Network Utilization
    By Faisal Ahmed in forum Networking
    Replies: 0
    Last Post: 12-02-2009, 07:32 AM
  3. Replies: 0
    Last Post: 08-12-2009, 08:42 PM
  4. why the number will not increase?
    By rayda in forum New To Java
    Replies: 3
    Last Post: 03-16-2009, 06:47 AM
  5. Increase signature length?
    By xcallmejudasx in forum Suggestions & Feedback
    Replies: 3
    Last Post: 12-18-2008, 07:04 PM

Posting Permissions

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