ArrayList Demo

From Java Example Source Code

Jump to: navigation, search

Contents

[edit] Overview - ArrayList Demo

This is a example of Java program.

[edit] Java Source Code

  • Package: com.exampleshow.util
  • File: ArrayListDemo.java
package com.exampleshow.util;
 
/**
 * @author Code Panda
 * Copyright (C) 2008 Java ExampleShow (http://java.exampleshow.com)
 *
 * This code is free software; you can redistribute it and/or modify it.
 * However, this header must remain intact and unchanged. Additional
 * information may be appended after this header. Publications based on
 * this code must also include an appropriate reference.
 * 
 * This code is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
 * or FITNESS FOR A PARTICULAR PURPOSE.
 *
 */
 
import java.util.ArrayList;
 
/**
 * ArrayListDemo done over using an ArrayList
 */
public class ArrayListDemo {
    public static void main(String[] argv) {
	ArrayList al = new ArrayList();
 
	// Add lots of elements to the ArrayList...
	for (int i = 0; i < 20; i++) {
	    al.add(i + ":" + Math.random());
	}
	// First print them out using a for loop.
	System.out.println("Retrieving by index:");
	for (int i = 0; i < al.size(); i++) {
	    System.out.println("Element " + i + " = " + al.get(i));
	}
    }
}

[edit] What Result You Can Get

Run the program, you may get:


Retrieving by index:
Element 0 = 0:0.5224051248443833
Element 1 = 1:0.230922378286891
Element 2 = 2:0.13906974899377056
Element 3 = 3:0.4257783025193548
Element 4 = 4:0.8861976636337451
Element 5 = 5:0.5997751267941356
Element 6 = 6:0.7843386132074464
Element 7 = 7:0.663588165491043
Element 8 = 8:0.7851417298864475
Element 9 = 9:0.02817403752611569
Element 10 = 10:0.17831021335033415
Element 11 = 11:0.22310792055710804
Element 12 = 12:0.27847962447031915
Element 13 = 13:0.35499303580016817
Element 14 = 14:0.4988144764413345
Element 15 = 15:0.5135402474228805
Element 16 = 16:0.05889866640954999
Element 17 = 17:0.41535306292228524
Element 18 = 18:0.771491286956049
Element 19 = 19:0.5665463837474979

Because element's value is created by Math.random() randomly, so, the value is different every time.

[edit] Required External Library for this Java Example

Need nothing.


[edit] How to Run this Java Example Program

We recommend running this Java example program with Eclipse.

For assistance in working with Eclipse, please see How to Run Java Program with Eclipse.

It's fairly easy.



[edit] Question & Answer

Any question?

Click edit and post your question or answer here.


Personal tools