Results 1 to 6 of 6
  1. #1
    ankiit is offline Member
    Join Date
    Dec 2011
    Location
    India
    Posts
    74
    Rep Power
    0

    Default How to declare an ArrayList that can hold int variables

    Hi All,

    Please share the way of How to declare an ArrayList that can hold int variables?

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,569
    Blog Entries
    7
    Rep Power
    21

    Default Re: How to declare an ArrayList that can hold int variables

    ArrayList<Integer>

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    ankiit is offline Member
    Join Date
    Dec 2011
    Location
    India
    Posts
    74
    Rep Power
    0

    Default Re: How to declare an ArrayList that can hold int variables

    Hi Jos,

    Thanks.

    But I want to use something like:-
    Java Code:
    int rand = (int) (Math.random()* 5);
    ArrayList<Integer> loc = {rand, rand+1, rand+2};

  4. #4
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    356
    Rep Power
    5

    Default Re: How to declare an ArrayList that can hold int variables

    An ArrayList is different from an Array.
    You need to initialize your ArrayList first, then add your elements to the ArrayList.

    Java Code:
     ArrayList<Integer> loc = new ArrayList<Integer>();
    loc.add(rand);
    loc.add(rand+1);
    loc.add(rand+2);
    Last edited by sehudson; 03-26-2012 at 09:45 PM.

  5. #5
    ankiit is offline Member
    Join Date
    Dec 2011
    Location
    India
    Posts
    74
    Rep Power
    0

    Default Re: How to declare an ArrayList that can hold int variables

    thanks i got your point.

  6. #6
    Garik is offline Member
    Join Date
    Aug 2014
    Posts
    1
    Rep Power
    0

    Default Re: How to declare an ArrayList that can hold int variables

    Quote Originally Posted by ankiit View Post
    Hi Jos,

    Thanks.

    But I want to use something like:-
    Java Code:
    int rand = (int) (Math.random()* 5);
    ArrayList<Integer> loc = {rand, rand+1, rand+2};
    Head first java i guess!

Similar Threads

  1. Replies: 10
    Last Post: 03-05-2012, 05:44 AM
  2. Texas Hold'em Poker
    By JohnPringle83 in forum Java Gaming
    Replies: 0
    Last Post: 05-05-2011, 05:40 AM
  3. Replies: 2
    Last Post: 11-16-2010, 05:51 PM
  4. Replies: 11
    Last Post: 04-01-2009, 03:43 AM
  5. How to declare and initalize application space variables
    By thagentleman in forum New To Java
    Replies: 2
    Last Post: 10-11-2008, 07:19 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
  •