Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: How to use an ArrayList and what is its advantage over array?

  1. #1
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Post How to use an ArrayList and what is its advantage over array?

    A lot of people ask me how to use an ArrayList and also ask what is the advantage of an ArrayList over a standard Array.

    If the data has a known number of elements or small fixed size upper bound, arrays are often the best choice. However, many data storage problems are not that simple. The main advantage to an ArrayList is that it automatically expands as data is added. A disadvantage is that an ArrayList cannot work with primitive types.

    Here is a quick example to show you how to add, print, and remove data from an ArrayList:

    // Import ArrayList
    import java.util.ArrayList;
     
    public class ArrayListExample {
     
        /**
         * JavaProgrammingForums.com
         */
        public static void main(String[] args) {
     
            // Construct a new empty ArrayList for type String
            ArrayList<String> al = new ArrayList<String>();
     
            // Fill ArrayList (index, String)
            al.add(0, "Java");
            al.add(1, "Programming");
            al.add(2, "Forums");
            al.add(3, ".com");
     
            // Convert ArrayList to Object array
            Object[] elements = al.toArray();
     
            // Print Object content
            for (int a = 0; a < elements.length; a++) {
                System.out.println(elements[a]);
            }
     
            // Remove item from ArrayList
            al.remove(3);
     
            // Clear entire ArrayList
            al.clear();
        }
     
    }
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  2. The Following 2 Users Say Thank You to JavaPF For This Useful Post:

    ShaunB (October 25th, 2011), xmode (November 22nd, 2012)


  3. #2
    Member Fendaril's Avatar
    Join Date
    Nov 2008
    Posts
    54
    Thanks
    7
    Thanked 6 Times in 5 Posts

    Default Re: How to use an ArrayList - java.util.ArrayList

    You forgot to add that ArrayLists cannot work with primitive types.

  4. The Following 2 Users Say Thank You to Fendaril For This Useful Post:

    JavaPF (May 17th, 2009), xmode (November 22nd, 2012)

  5. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to use an ArrayList - java.util.ArrayList

    thanks alot
    it is working

  6. #4
    Junior Member
    Join Date
    Dec 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Old but very good tutorial.

  7. #5
    Member
    Join Date
    Nov 2011
    Posts
    39
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: How to use an ArrayList - java.util.ArrayList

    Quote Originally Posted by JavaPF View Post
    A disadvantage is that an ArrayList cannot work with primitive types.
    It works since Java 5 via autoboxing/unboxing feature. This understand is old and should be replaced.

    Spring 3
    Last edited by cafeteria84; January 22nd, 2012 at 04:06 PM.

Similar Threads

  1. How to Sort an Array using the java.util.Arrays class
    By JavaPF in forum Java SE API Tutorials
    Replies: 2
    Last Post: May 17th, 2014, 01:16 AM
  2. Replies: 3
    Last Post: April 26th, 2011, 02:51 AM
  3. convert arraylist to a hash map
    By nadman123 in forum Collections and Generics
    Replies: 1
    Last Post: July 29th, 2009, 04:24 AM
  4. Object creation from a input file and storing in an Array list
    By LabX in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: May 14th, 2009, 03:52 AM
  5. Replies: 4
    Last Post: April 29th, 2009, 10:53 AM

Tags for this Thread