Results 1 to 8 of 8

Thread: new array ?

  1. #1
    seandingobat is offline Member
    Join Date
    Sep 2008
    Posts
    16
    Rep Power
    0

    Default new array ?

    how do you declare a new int array?

    like int[] array; ???


    then how do you add to the array??

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    You should learn the basis about Java, this is simple thing actually.

    Java Code:
    public class IntArray {
    
        public static void main(String[] args) {
            int temp = 0;  // Dummy value
            
            int[] myArray; // Declaring an int type array
            myArray = new int[10]; // Initialize the array and reserver the space
                                   // for 10 elements.
            
            // Adding elements to the array
            for(int i = 0; i < myArray.length; i++) {
                temp += 5;
                myArray[i] = temp;
            }
            
            // Display elements
            for(int j = 0; j < myArray.length; j++) {
                System.out.println("Value of index " + j + " is " + myArray[j]);
            }
        }
    
    }

  3. #3
    seandingobat is offline Member
    Join Date
    Sep 2008
    Posts
    16
    Rep Power
    0

    Default

    thanks for replying!

    what if i don't know the size of my array and i just want to be able to add an arbitrary amount to it.

    instead of freeing up a fixed size space, how would i go about this?

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    You want to set the dynamic size. Arrays not allowed that, you have to follow another way. Read more about Java collections. There are lots of options.

  5. #5
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    Use an java.util.ArrayList. How? Find it out yourself, no spoon feeding. ;)
    I die a little on the inside...
    Every time I get shot.

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Actually you have lots of choices, ArrayList is just a one of them. As Supamagier says, finding yourself is your duty. We'll help you, just have atry.

  7. #7
    kellaw is offline Member
    Join Date
    Oct 2008
    Posts
    24
    Rep Power
    0

    Default

    else use list. it would be easier

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by kellaw View Post
    else use list. it would be easier
    That's why we are advising to read more on Java docs. There are lots of ways. More important thing is, still no response from our thread starter.

Similar Threads

  1. 3 by 3 array
    By fromme87 in forum New To Java
    Replies: 5
    Last Post: 10-02-2008, 08:34 AM
  2. help with an array.
    By f_the_cook in forum New To Java
    Replies: 7
    Last Post: 06-03-2008, 04:05 AM
  3. Array Reflection: Multi Array Reflection
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-23-2008, 08:08 PM
  4. Would appreciate your help with 2d Array..
    By cloudkicker in forum New To Java
    Replies: 1
    Last Post: 02-11-2008, 02:34 PM
  5. 2D array
    By bluekswing in forum New To Java
    Replies: 2
    Last Post: 01-15-2008, 05:57 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
  •