Go Back   Java Programming Forums > Learning Java > Java Code Snippets and Tutorials

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 15-05-2009, 01:34 PM
JavaPF's Avatar
mmm.. coffee
 
5 Highscores

Join Date: May 2008
Location: United Kingdom
Posts: 1,210
Thanks: 60
Thanked 66 Times in 64 Posts
JavaPF is someone you want to know!JavaPF is someone you want to know!JavaPF is someone you want to know!

I'm feeling Inspired
Post How to use an ArrayList - java.util.ArrayList

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:

Java Code
// 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();
    }

}



__________________
Don't forget to add code tags around your code:

Forum Tip: Add to peoples reputation () by clicking the button on their useful posts.
Reply With Quote Share this thread on Facebook
Sponsored Links
  #2 (permalink)  
Old 17-05-2009, 06:12 PM
Fendaril's Avatar
Member
 
4 Highscores

Join Date: Nov 2008
Posts: 54
Thanks: 7
Thanked 5 Times in 5 Posts
Fendaril is on a distinguished road
Default Re: How to use an ArrayList - java.util.ArrayList

You forgot to add that ArrayLists cannot work with primitive types.
Reply With Quote
The Following User Says Thank You to Fendaril For This Useful Post:
JavaPF (17-05-2009)
Reply

Tags
arraylist

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



Similar Threads
Thread Thread Starter Forum Replies Last Post
convert arraylist to a hash map nadman123 Collections and Generics 1 29-07-2009 09:24 AM
creating objects from file input and storing in arraylist LabX File I/O & Other I/O Streams 4 14-05-2009 08:52 AM
java.util.UnknownFormatConversionException error rosh72851 Exceptions 4 29-04-2009 03:53 PM
How to Sort an Array using the java.util.Arrays class JavaPF Java Code Snippets and Tutorials 0 01-12-2008 02:02 PM
java.util.UnknownFormatConversionException error rosh72851 Exceptions 2 08-10-2008 04:01 PM


100 most searched terms
Search Cloud
2 dimensional arraylist java 2d arraylist java actionlistener actionlistener in java actionlistener java actionlistener jbutton addactionlistener addactionlistener java avatar hardware id convert double to integer java double format java double to int java double to integer in java double to integer java eclipse shortcut keys eclipse tutorial for beginners exception in thread "awt-eventqueue-0" java.lang.outofmemoryerror: java heap space exception in thread "main" java.lang.outofmemoryerror: java heap space format double java get mouse position java hardware id avatar java 2 dimensional arraylist java 2d arraylist java actionlistener java addactionlistener java button actionlistener java convert double to int java double format java double to int java double to integer java for beginner eclippse java format double java forum java forums java get mouse position java ipod touch java jbutton java list to map java mouse position java programming forum java programming forums java sendkeys java.lang.reflect.invocationtargetexception java.util.arraylist jbutton actionlistener jbutton java programming forums string to int java two dimensional arraylist java writing apps for ipod touch

All times are GMT. The time now is 01:00 PM.
Powered by vBulletin® Copyright ©2000-2009, Jelsoft Enterprises Ltd.