Share on Google+Share on Google+

Manoj Hunasigi
collections
1 Answer(s)      6 years ago
Posted in : Java Beginners

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("[email protected]");
    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

Ads

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  why we need collections.when we will use collections in our applications
Advertisements
collections
collections  in collections can we perform binarysearch on arraylist elements without sorting them
what is collections?
what is collections?  what is collections
Collections API
Collections API  hello, What is the Collections API?   hi 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
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
java collections
achieve this plz without using stringtokenizer with any 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
collections - Java Interview Questions
collections   what is the way to insert map values into a set in collections
Tutorials   
Java Spring Hibernate Struts Training Retrieve database from the table dynamically in jsp from oracle using servlet What does core Java include? java What are some way to learn Java quickly? Is it required to learn Java before learning Java Script? Is it necessary to learn java script before learning PHP? Are HTML5 and Java Script boosts Java career? Is Java object oriented? Fingerprint application with Java Uninstall Oracle Virtual Box JSON to HashMap Free Java online Training I want example of Control Statement in Java ANSI Color Codes with Python Create a Program that Calculates Input What is difference between JDK,JRE and JVM? How to see ubuntu version on server? How to get Page Source in Selenium (WebDriver) using Java? The path to the driver executable must be set by the webdriver.gecko.driver system property parse data from a link in java Java Program Qns using BlueJ How to fix HAX Kernel Module Is Not Installed error? Installing Audacity Looking for code Logic to check track changes & Coments in MSWord, MSWordx, MSExcel, MSExcelx is ON/OFF Installing JDK on Mac SAX Parser exception ERROR 601 (42P00): Syntax error. Encountered Thread java.lang.NoClassDefFoundError: org/apache/commons/fileupload/FileItemFactory How to install Ubuntu 16.04 LTS? HttpServletRequest cannot be resolved to a type in eclipse - Solved Unhandled event loop exception GC overhead limit exceeded Spring Data jpa with apache phoenix Caused by: java.lang.IllegalArgumentException: Not a host:port pair: PBUF o.a.h.h.z.RecoverableZooKeeper - Possibly transient ZooKeeper, quorum= com.thinkaurelius.titan.diskstorage.hbase.HBaseStoreManager class not found com.thinkaurelius.titan.diskstorage.hbase.HBaseStoreManager not found How to download and install Java 8 on Windows? How to uninstall JDK 7? How to install gtk-doc-tools package in Ubuntu? How to install Oracle JDK 8 on Ubuntu? ejabberd_ctl.beam not found - Solved How to convert date to UTC format in Java? How to install autoconf, automake and libtool in Ubuntu 15.10? How to convert current date to mm dd yyyy format in Java? How to convert current date to dd mm yyyy format in Java? How to stop window closing in "internalFrameClosing" event. How to find list of all index in Neo4j? neo4j-server.properties file location SASLError using PLAIN: not-authorized

Ads

 
Advertisement null

Ads