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
 
7 Highscores

Join Date: May 2008
Location: United Kingdom
Posts: 1,581
Thanks: 103
Thanked 93 Times in 86 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 Stressed
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 syntax highlighted code tags around your code: [highlight=Java] code here [/highlight]

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

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 01:02 PM
java.util.UnknownFormatConversionException error rosh72851 Exceptions 2 08-10-2008 04:01 PM


100 most searched terms
Search Cloud
2d arraylist java actionlistener actionlistener in java actionlistener java addactionlistener addactionlistener in java addactionlistener java applications of oops could not create java virtual machine xp double format java double to int 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.nullpointerexception exception in thread "main" java.lang.outofmemoryerror: java heap space format double java get mouse position java java 2d arraylist java actionlistener java addactionlistener java convert list to map java double format java double formatting java double to int java double to integer java format double java forum java forums java get mouse position java list to map java mouse position java programming forum java programming forums java programming help java sendkeys java two dimensional arraylist java.lang.classformaterror: truncated class file java.lang.outofmemoryerror: java heap space java.util.arraylist jbutton actionlistener jtextarea font jtextfield font size jxl.read.biff.biffexception: unable to recognize ole stream programming mutators and generics two dimensional arraylist java writing ipod apps

All times are GMT. The time now is 07:13 AM.
Powered by vBulletin® Copyright ©2000-2009, Jelsoft Enterprises Ltd.