collections

collections

please explain me the flow of this prog


import java.util.*;

public class listinterface {


/**
 * @param args
 */
public static void main(String as[]) {
    ArrayList al= new ArrayList();
    al.add(new Integer(99));
    al.add("sri");
    al.add("123");
    al.add("sri@jlc");
    al.add("sri");
    System.out.println(al);
    al.add(0,"aaaa");
    al.add(2,"bbbb");
    al.remove(4);
    System.out.println(al);
    System.out.println(al.get(2));
    System.out.println(al.indexOf("Sri"));
    System.out.println(al.lastIndexOf("sri"));
    al.set(0,"jlc");
    System.out.println(al);
    List list=al.subList(1,4);
    System.out.println(list);
    System.out.println("forward order");
    ListIterator li= al.listIterator();
    while(li.hasNext()){
        System.out.println(li.next());
    }
    System.out.println("reverse order");
    while(li.hasPrevious()){
        System.out.println(li.previous());
    }
}

}

Thanks

View Answers

April 19, 2011 at 1:15 PM

In the given code, an object of ArrayList is created. The method add() then adds an integer value and four string values to the array list. And then System.out.println(al) displays the array list values. The method al.add(0,"aaaa") adds an element "aaaa" at the postion 0 to the list and the al.add(2,"bbbb") adds the element "bbbb" at the position 2 to the list. The method remove(4) removes the element specified at position 4 from the list. The method get(2) displays the element of the list whose position is 2. As there is no element "Sri" so the method indexOf("Sri") returns -1. The method lastIndexOf("Sri") returns the last occurring index of the element from the list. The method set(0,"jlc") replaces the element specified at the postion 0 with the "jlc". The ListIterator is an iterator for lists that allows the programmer to traverse the list in either direction. The method next() returns the next element in the list or you can say display the list of elements in forward direction. The method previous() of this class returns the previous element in the list or you can say in backward direction.









Related Tutorials/Questions & Answers:
Collections
Collections  Hi, Please send a study material on collections.../java/jdk6/introduction-collections-api.shtml http://www.roseindia.net/javacodeexamples/index.shtml http://www.devmanuals.com/tutorials/java/collections/index.html
collections
collections  in collections can we perform binarysearch on arraylist elements without sorting them
Advertisements
collections
collections  why we need collections.when we will use collections in our applications
what is collections?
what is collections?  what is collections
Collections API
Collections API  hello, What is the Collections API?   hiADS_TO_REPLACE_1 The Collections API is a set of classes and interfaces that support operations on collections of objects
Collections in Java
Collections in Java  What is Concurrent Hash map
Collections in Java
Collections in Java  What is Concurrent Hash map
java collections
java collections  what are all the methods and things added for collections in java5 and java6 please kindly reply me as soon as possible
Version of commons-collections>commons-collections dependency
List of Version of commons-collections>commons-collections dependency
Version of excalibur-collections>excalibur-collections dependency
List of Version of excalibur-collections>excalibur-collections dependency
Version of commons-collections>commons-collections-testframework dependency
List of Version of commons-collections>commons-collections-testframework dependency
Collections Framework
Collections Framework  Sir, We know that both HashMap & Hashtable is using for same purposes i.e Used for storing keys-values pair. But there is some difference between this two class that are 1)Hashtable is synchronized
collections in java
collections in java  please explain me the flow of this program..i m getting o/p as 2 1 1..y not 2 1 0..is it that if we change the i2 value,it doesnt remove. import java.util.*; public class Mapit { public static void main
collections
collections
collections
collections
collections
collections
Collections
collections
collections
collections
Collections
collections
collections
collections
collections
Collections
collections
collections
java collections
achieve this plz without using stringtokenizer with any collections!!!  
collections - Java Interview Questions
collections   what is the way to insert map values into a set in collections
Hibernate Criteria With Collections Example
Hibernate Criteria With Collections Example  Good code of Hibernate Criteria With Collections Example. Thanks   Example program of using Hibernate Criteria With Collections in your Java project. Check the tutorial
Version of com.jonlandrum>collections dependency
List of Version of com.jonlandrum>collections dependency
Version of com.twineworks>collections dependency
List of Version of com.twineworks>collections dependency
collections in java - Java Interview Questions
collections in java  What is collection
Artifacts of commons-collections
List of Artifacts of commons-collections maven depenency
Artifacts of excalibur-collections
List of Artifacts of excalibur-collections maven depenency
Maven Repository/Dependency: excalibur-collections | excalibur-collections
Maven Repository/Dependency of Group ID excalibur-collections and Artifact ID excalibur-collections. Latest version of excalibur-collections:excalibur-collections dependencies. # Version Release Date
Maven Repository/Dependency: com.googlecode.functional-collections | functional-collections
Maven Repository/Dependency of Group ID com.googlecode.functional-collections and Artifact ID functional-collections. Latest version of com.googlecode.functional-collections:functional-collections dependencies
Maven Repository/Dependency: com.lexicalscope.fluent-collections | fluent-collections
Maven Repository/Dependency of Group ID com.lexicalscope.fluent-collections and Artifact ID fluent-collections. Latest version of com.lexicalscope.fluent-collections:fluent-collections dependencies. # Version
Maven Repository/Dependency: com.jnape.dynamic-collections | dynamic-collections
Maven Repository/Dependency of Group ID com.jnape.dynamic-collections and Artifact ID dynamic-collections. Latest version of com.jnape.dynamic-collections:dynamic-collections dependencies. # Version Release
collections - Java Interview Questions
collections  The Java Collections API  Hi friend, Java Collections of API (Application Programming Intreface) Consists of several.../java/jdk6/introduction-collections-api.shtml Thanks
Maven Repository/Dependency: commons-collections | commons-collections
Maven Repository/Dependency of Group ID commons-collections and Artifact ID commons-collections. Latest version of commons-collections:commons-collections dependencies. # Version Release Date
Maven Dependency commons-collections >> 1.0
You should include the dependency code given in this page to add Maven Dependency of commons-collections >> commons-collections version1.0 in your project
Maven Dependency commons-collections >> 2.0
You should include the dependency code given in this page to add Maven Dependency of commons-collections >> commons-collections version2.0 in your project
Maven Dependency commons-collections >> 20030418.083655
You should include the dependency code given in this page to add Maven Dependency of commons-collections >> commons-collections version20030418.083655 in your project
Maven Dependency commons-collections >> 20031027.000000
You should include the dependency code given in this page to add Maven Dependency of commons-collections >> commons-collections version20031027.000000 in your project
Maven Dependency commons-collections >> 20040102.233541
You should include the dependency code given in this page to add Maven Dependency of commons-collections >> commons-collections version20040102.233541 in your project

Ads