Results 1 to 11 of 11

Thread: ArrayList

  1. #1
    f22raptor is offline Member
    Join Date
    Aug 2011
    Posts
    16
    Rep Power
    0

    Default ArrayList

    Hi,

    My first question is how do i declare arraylist of primitives? I mean can i declare Arraylist of floats, char, boolean?? If yes then what is the correct way of doing that?? And my second questions is that i tried declaring an array list of float like this below.

    Java Code:
    import java.util.ArrayList;
    
    class Arrayfloat
    {
      public static void main(String[] args)
      {
        ArrayList<Float> my = new ArrayList<Float>();
      }
    }
    And that above code throws me a weird error like "variable n might not have been intialized. System.out.println(n);". The point is that no where in the above code is n mentioned.

    Kindly help me out.

    Thank you.
    Last edited by sunde887; 08-07-2011 at 06:47 AM. Reason: Added code tags, [code]...[/code]

  2. #2
    yellowledbet is offline Senior Member
    Join Date
    Feb 2011
    Location
    Georgia, USA
    Posts
    122
    Rep Power
    0

    Default

    Are you sure that is all of your code?

    Ways to help those that may be willing to help you:
    1. Use code tags
    2. Format your code
    3. Provide the full error message

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Providing the full error message is helpful in situations like these. Vague questions lead to guessed answers.

    Generally speaking you can't create a list with a primitive generic type(List<int> is not allowed), however, you can use the boxed classes such as Float, Integer, Double, etc. and rely on auto-boxing and unboxing to allow primitives to be added.

    Java Code:
    List<Integer> ints = new ArrayList<Integer>();
    ints.add(4);

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,353
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by f22raptor View Post
    The point is that no where in the above code is n mentioned.
    Check what code got actually compiled; it most certainly isn't the code you've shown us.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Hibernate's Avatar
    Hibernate is offline Senior Member
    Join Date
    Dec 2010
    Location
    Stockholm, Sweden
    Posts
    222
    Blog Entries
    9
    Rep Power
    4

    Default

    Quote Originally Posted by sunde887 View Post
    Providing the full error message is helpful in situations like these. Vague questions lead to guessed answers.

    Generally speaking you can't create a list with a primitive generic type(List<int> is not allowed), however, you can use the boxed classes such as Float, Integer, Double, etc. and rely on auto-boxing and unboxing to allow primitives to be added.

    Java Code:
    List<Integer> ints = new ArrayList<Integer>();
    ints.add(4);
    List<Integer> ints = new ArrayList<Integer>();
    Or
    ArrayList<Integer> ints = new ArrayList<Integer>();

    Is the proper why to go, but you can also use
    ArrayList<int[]> ints = new ArrayList<int[]>();
    Where all int[] holds exactly one element, this should have
    about the same speed, maybe (I don't know) slightly faster for reading
    the values. But the real plus it that it consumes haft the amount of
    memory making it preferable if you are dealing with loads (a huge amount) of data.
    (Arrays handled as objects, and therefore extends Object, but they are final, so you can't extend them [the arrays, like int[]].)


    However I think auto-boxing is ugly; always use explicit boxing: new Integer(4).
    Last edited by Hibernate; 08-07-2011 at 08:12 AM.
    Ex animo! Hibernate
    Java, Arch Linux, C, GPL v3, Bash, Eclipse, Linux VT, GNOME 2 and many buttons on windows.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,353
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Hibernate View Post
    ArrayList<int[]> ints = new ArrayList<int[]>();
    Where all int[] holds exactly one element, this should have
    about the same speed, maybe (I don't know) slightly faster for reading
    the values. But the real plus it that it consumes haft the amount of
    memory making it preferable if you are dealing with loads (a huge amount) of data.
    (Arrays handled as objects, and therefore extends Object, but they are final, so you can't extend them [the arrays, like int[]].)


    However I think auto-boxing is ugly; always use explicit boxing: new Integer(4).
    Nope, use Integer.valueOf(4) so a previous instantiation of Integer(4) can be used; I also don't believe the memory requirements: both an int[] and an Integer are Objects so they take up the same amount of bytes. Arrays also do bounds checking which makes them a bit slower ... I prefer to use Integer wrappers.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    Hibernate's Avatar
    Hibernate is offline Senior Member
    Join Date
    Dec 2010
    Location
    Stockholm, Sweden
    Posts
    222
    Blog Entries
    9
    Rep Power
    4

    Default

    I think I just replied, that happened to the reply?
    I'll rewrite it (very) briefly then…

    I forgot about the bound checking. Also using valueOf, is better, which I also forgot because I have not done this for two years,
    the only thing I have done is using Boolean, but not involving generics.

    I think the [standard] JRE does not store arrays as objects, but otherwise handles them as such.
    I think they have an address α and that myarray[i] have the address α + i. So the first elements
    address is stored and the number of elements, meaning that the only actual objects are the elements (if they are not primitive).
    Ex animo! Hibernate
    Java, Arch Linux, C, GPL v3, Bash, Eclipse, Linux VT, GNOME 2 and many buttons on windows.

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,353
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Hibernate View Post
    I think the [standard] JRE does not store arrays as objects
    Yes it does; it's just the language that forbids extending from any array class but such classes extend from the Object class (it even overrides the clone() method and implements the Cloneable and Serializable interfaces). The 'raw' data of arrays are treated as you described (wrapped in bounds checking).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    Hibernate's Avatar
    Hibernate is offline Senior Member
    Join Date
    Dec 2010
    Location
    Stockholm, Sweden
    Posts
    222
    Blog Entries
    9
    Rep Power
    4

    Default

    So it is stored as a regular object, and things are not handled natively (with exception for Serializable)?
    Ex animo! Hibernate
    Java, Arch Linux, C, GPL v3, Bash, Eclipse, Linux VT, GNOME 2 and many buttons on windows.

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,353
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Hibernate View Post
    So it is stored as a regular object, and things are not handled natively (with exception for Serializable)?
    I don't understand what you wrote aboute 'natively' handling array and Serializable stuff. The JVM has a few instructions for arrays which can be further compiled to raw machine code by the JIT; the largest problem is determining whether or not it is safe to leave out the bounds checking. Serialization is also implemented in just Java; see the src.zip file for the ObjectOutputStream and ObjectInputStream. Those too can be further compiled by the JIT compiler.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    Hibernate's Avatar
    Hibernate is offline Senior Member
    Join Date
    Dec 2010
    Location
    Stockholm, Sweden
    Posts
    222
    Blog Entries
    9
    Rep Power
    4

    Default Re: ArrayList

    Quote Originally Posted by JosAH View Post
    Arrays also do bounds checking which makes them a bit slower ...
    I've recently benchmarked it, and ArrayList<int[]> is c:a twice as fast as ArrayList<Integer>.
    Ex animo! Hibernate
    Java, Arch Linux, C, GPL v3, Bash, Eclipse, Linux VT, GNOME 2 and many buttons on windows.

Similar Threads

  1. Replies: 12
    Last Post: 07-07-2011, 08:49 PM
  2. copying contents of an ArrayList to another ArrayList
    By ankit1801 in forum New To Java
    Replies: 8
    Last Post: 03-27-2011, 06:07 AM
  3. sorting arraylist based on another arraylist
    By busdude in forum New To Java
    Replies: 4
    Last Post: 02-07-2011, 11:48 AM
  4. Replies: 7
    Last Post: 11-22-2010, 01:26 PM
  5. Replies: 2
    Last Post: 04-21-2008, 11: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
  •