java.lang.Object
java.util.AbstractCollection<E>
java.util.AbstractList<E>
java.util.ArrayList<E>
- All Implemented Interfaces:
- Serializable, Cloneable, Iterable<E>, Collection<E>, List<E>, RandomAccess
- Direct Known Subclasses:
- AttributeList, RoleList, RoleUnresolvedList
- See Also:
- Top Examples, Source Code,
LinkedList ,
Vector ,
Collections.synchronizedList(List)
public boolean add(E o) - See Also:
- AbstractList, List, Collection
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[1963]converting array to list and list to arrays By hemala on 2008/04/03 00:15:10 Rate
//string array String s=new String ( "this ,is ,my ,first ,example" ) ; String a [ ] =s.split ( "\\," ) ; for ( int i=0;i < a.length;i++ ) System.out.println ( a [ i ] ) ; //converting string array to list List l=new ArrayList ( ) ; for ( int i=0;i < a.length;i++ ) l.add ( a [ i ] ) ; System.out.println ( "string array to list"+l ) ; //converting list to string array String aa [ ] = ( String [ ] ) l.toArray ( new String [ l.size ( ) ] ) ; //String aa [ ] =new String [ l.size ( ) ] ; //aa= ( String [ ] ) l.toArray ( ) ; for ( int i=0;i < aa.length;i++ ) System.out.println ( aa [ i ] ) ; [2012]GO By Anonymous on 2010/07/08 16:48:19 Rate
import java.util.List; import java.util.Stack; import java.util.Vector; public class RandomCreditCardNumberGenerator { public static final String [ ] VISA_PREFIX_LIST = new String [ ] { "4539", "4556", "4916", "4532", "4929", "40240071", "4485", "4716", "4" } ; public static final String [ ] MASTERCARD_PREFIX_LIST = new String [ ] { "51", "52", "53", "54", "55" } ; public static final String [ ] AMEX_PREFIX_LIST = new String [ ] { "34", "37" } ; public static final String [ ] DISCOVER_PREFIX_LIST = new String [ ] { "6011" } ; public static final String [ ] DINERS_PREFIX_LIST = new String [ ] { "300", "301", "302", "303", "36", "38" } ; public static final String [ ] ENROUTE_PREFIX_LIST = new String [ ] { "2014", "2149" } ; public static final String [ ] JCB_16_PREFIX_LIST = new String [ ] { "3088", "3096", "3112", "3158", "3337", "3528" } ; public static final String [ ] JCB_15_PREFIX_LIST = new String [ ] { "2100", "1800" } ; public static final String [ ] VOYAGER_PREFIX_LIST = new String [ ] { "8699" } ; static String strrev ( String str ) { if ( str == null ) return ""; String revstr = ""; for ( int i = str.length ( ) - 1; i > = 0; i-- ) { revstr += str.charAt ( i ) ; } return revstr; } static String completed_number ( String prefix, int length ) { String ccnumber = prefix; // generate digits while ( ccnumber.length ( ) < ( length - 1 ) ) { ccnumber += new Double ( Math.floor ( Math.random ( ) * 10 ) ) .intValue ( ) ; } // reverse number and convert to int String reversedCCnumberString = strrev ( ccnumber ) ; List < Integer > reversedCCnumberList = new Vector < Integer > ( ) ; for ( int i = 0; i < reversedCCnumberString.length ( ) ; i++ ) { reversedCCnumberList.add ( new Integer ( String .valueOf ( reversedCCnumberString.charAt ( i ) ) ) ) ; } // calculate sum int sum = 0; int pos = 0; Integer [ ] reversedCCnumber = reversedCCnumberList .toArray ( new Integer [ reversedCCnumberList.size ( ) ] ) ; while ( pos < length - 1 ) { int odd = reversedCCnumber [ pos ] * 2; if ( odd > 9 ) { odd -= 9; } sum += odd; if ( pos != ( length - 2 ) ) { sum += reversedCCnumber [ pos + 1 ] ; } pos += 2; } // calculate check digit int checkdigit = new Double ( ( ( Math.floor ( sum / 10 ) + 1 ) * 10 - sum ) % 10 ) .intValue ( ) ; ccnumber += checkdigit; return ccnumber; } public static String [ ] credit_card_number ( String [ ] prefixList, int length, int howMany ) { Stack < String > result = new Stack < String > ( ) ; for ( int i = 0; i < howMany; i++ ) { int randomArrayIndex = ( int ) Math.floor ( Math.random ( ) * prefixList.length ) ; String ccnumber = prefixList [ randomArrayIndex ] ; result.push ( completed_number ( ccnumber, length ) ) ; } return result.toArray ( new String [ result.size ( ) ] ) ; } public static String [ ] generateMasterCardNumbers ( int howMany ) { return credit_card_number ( MASTERCARD_PREFIX_LIST, 16, howMany ) ; } public static String generateMasterCardNumber ( ) { return credit_card_number ( MASTERCARD_PREFIX_LIST, 16, 1 ) [ 0 ] ; } public static boolean isValidCreditCardNumber ( String creditCardNumber ) { boolean isValid = false; try { String reversedNumber = new StringBuffer ( creditCardNumber ) .reverse ( ) .toString ( ) ; int mod10Count = 0; for ( int i = 0; i < reversedNumber.length ( ) ; i++ ) { int augend = Integer.parseInt ( String.valueOf ( reversedNumber .charAt ( i ) ) ) ; if ( ( ( i + 1 ) % 2 ) == 0 ) { String productString = String.valueOf ( augend * 2 ) ; augend = 0; for ( int j = 0; j < productString.length ( ) ; j++ ) { augend += Integer.parseInt ( String.valueOf ( productString .charAt ( j ) ) ) ; } } mod10Count += augend; } if ( ( mod10Count % 10 ) == 0 ) { isValid = true; } } catch ( NumberFormatException e ) { } return isValid; } public static void main ( String [ ] args ) { int howMany = 0; try { howMany = Integer.parseInt ( args [ 0 ] ) ; } catch ( Exception e ) { System.err .println ( "Usage error. You need to supply a numeric argument ( ex: 500000 ) " ) ; } String [ ] creditcardnumbers = generateMasterCardNumbers ( howMany ) ; for ( int i = 0; i < creditcardnumbers.length; i++ ) { System.out.println ( creditcardnumbers [ i ] + ":" + ( isValidCreditCardNumber ( creditcardnumbers [ i ] ) ? "valid" : "invalid" ) ) ; } } }
public void add(int index,
E element) - See Also:
- IndexOutOfBoundsException, AbstractList, List
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public void add(int index,
Object element) - See Also:
- IndexOutOfBoundsException, AbstractList, List
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[579]Populate list by inserting into first position By VentureKing on 2005/07/29 09:16:55 Rate
import java.util.ArrayList; class testArrayList { public static void main ( String [ ] args ) { ArrayList list1 = new ArrayList ( ) ; // Populate list by inserting into first position // pushing all existing elements down for ( int i = 5; i > 0; i-- ) { list1.add ( 0, "List item " + i ) ; } // Show list System.out.println ( "list1 size: " + list1.size ( ) ) ; for ( int i = 0; i < list1.size ( ) ; i++ ) { System.out.println ( list1.get ( i ) ) ; } } } [1147]How to Use ArrayList By mk_rauf { at } yahoo { dot } com on 2005/09/17 13:08:18 Rate
//If U go through this code U will understand how to Use ArrayList import java.util.*; public class UseArrayList { public static void main ( String [ ] args ) { //How to Use ArrayList //Begin ArrayList arraylist = new ArrayList ( ) ; String arr [ ] = { "india","","three",null,"new Integer ( 1 ) " } ; //Different ways to Add Elements to the arraylist for ( int i=0;i < arr.length;i++ ) { arraylist.add ( i,arr [ i ] ) ; } arraylist.add ( new Float ( 3.5 ) ) ; arraylist.add ( new StringBuffer ( "IN BUFFER" ) ) ; arraylist.add ( null ) ; arraylist.add ( new Float ( 3.5 ) ) ; String aa="Software Engineer"; arraylist.add ( 1,aa ) ; //To see the Elements of the arraylist System.out.println ( "The elements of arraylist are " ) ; for ( int i=0;i < arraylist.size ( ) ;i++ ) { System.out.println ( " "+ ( i+1 ) +" ) "+arraylist.get ( i ) ) ; } System.out.println ( "The size of arraylist ="+arraylist.size ( ) ) ; System.out.println ( "The arraylist is Empty? ="+arraylist.isEmpty ( ) ) ; //Set the element at position one arraylist.set ( 1,"J2EE Programmer" ) ; //Converts arraylist to array System.out.println ( "The elements of arraylist To array are " ) ; Object list2array [ ] = arraylist.toArray ( ) ; for ( int i=0;i < list2array.length;i++ ) { System.out.println ( " "+ ( i+1 ) +" ) "+list2array [ i ] ) ; } //Checks whether this object is present in the arraylist or not System.out.println ( "Contains this \"one\" value ="+arraylist.contains ( "one" ) ) ; System.out.println ( "Contains this \"null\" value ="+arraylist.contains ( null ) ) ; //Return the Index of the object //arraylist is zero based, so it will give position 4 System.out.println ( "IndexOf \"null\" value ="+arraylist.indexOf ( null ) ) ; //Gives the Last Occurance of the Object System.out.println ( "LastIndexOf \"new Float ( 3.5 ) \" value ="+arraylist.lastIndexOf ( new Float ( 3.5 ) ) ) ; //Remove the elements from the arraylist arraylist.remove ( 2 ) ; System.out.println ( "The size of arraylist ="+arraylist.size ( ) ) ; //arraylist.removeRange ( ) ; See removeRange method in the same site arraylist.clear ( ) ; System.out.println ( "The size of arraylist ="+arraylist.size ( ) ) ; //End of ArrayList } }
public boolean add(Object o) - See Also:
- AbstractList, List
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[573]Display contents of a list By VentureKing on 2004/09/03 20:09:01 Rate
import java.util.ArrayList; class testArrayList { public static void main ( String [ ] args ) { ArrayList list1 = new ArrayList ( ) ; // Populate list for ( int i = 0; i < 5; i++ ) { list1.add ( "List item " + i ) ; } // Show list for ( int i = 0; i < list1.size ( ) ; i++ ) { System.out.println ( list1.get ( i ) ) ; } } }
public boolean addAll(int index,
Collection<? extends E> c) - See Also:
- NullPointerException, IndexOutOfBoundsException, AbstractList, List
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public boolean addAll(Collection<? extends E> c) - See Also:
AbstractCollection.add(Object) , NullPointerException, List
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public ArrayList() - Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public ArrayList(int initialCapacity) - See Also:
- IllegalArgumentException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public ArrayList(Collection<? extends E> c) - See Also:
- NullPointerException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[316]Create JSP tag to display HTML Option list in Locale order By Anonymous on 2005/08/23 04:03:14 Rate
package com.ora.i18n.jsptags; import java.io.IOException; import java.text.Collator; import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.Locale; import javax.servlet.jsp.JspException; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.JspTagException; import javax.servlet.jsp.tagext.TagSupport; public class OptionList extends TagSupport { private Locale locale; private ArrayList optionList; public OptionList ( ) { locale = Locale.getDefault ( ) ; optionList = null; } public void setLocale ( Locale locale ) { this.locale = locale; } public void setOptionList ( ArrayList optionList ) { this.optionList = optionList; } public int doStartTag ( ) throws JspException { JspWriter out = pageContext.getOut ( ) ; try { Collator collator = Collator.getInstance ( locale ) ; Collections.sort ( optionList, collator ) ; Iterator sortedList = optionList.iterator ( ) ; while ( sortedList.hasNext ( ) ) { String option = ( String ) sortedList.next ( ) ; out.print ( " < OPTION value=\"" ) ; out.print ( option ) ; out.print ( "\" > " ) ; out.print ( option ) ; out.println ( " < /OPTION > " ) ; } } catch ( IOException ioe ) { throw new JspTagException ( ioe.toString ( ) ) ; } return SKIP_BODY; } public void release ( ) { super.release ( ) ; locale = Locale.getDefault ( ) ; optionList = null; } } [1784]easy way to sort an array of dates using arraylist By rr on 2006/07/07 14:28:09 Rate
ArrayList < Date > v = new ArrayList < Date > ( ) ; v.add ( isDate.checkIsDate ( "01-01-01","-",true ) ) ; v.add ( isDate.checkIsDate ( "01-02-01","-",true ) ) ; v.add ( isDate.checkIsDate ( "01-10-01","-",true ) ) ; v.add ( isDate.checkIsDate ( "01-9-01","-",true ) ) ; v.add ( isDate.checkIsDate ( "01-11-01","-",true ) ) ; Date [ ] myDateArray = ( Date [ ] ) v.toArray ( new Date [ v.size ( ) ] ) ; Arrays.sort ( myDateArray ) ; for ( int i =0; i < myDateArray.length;i++ ) { System.out.println ( myDateArray [ i ] ) ; }
public void clear() - See Also:
- E, AbstractList, List, Collection
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[576]Clear an arraylist By VentureKing on 2005/08/08 04:52:37 Rate
import java.util.ArrayList; class testArrayList { public static void main ( String [ ] args ) { ArrayList list1 = new ArrayList ( ) ; // Populate list for ( int i = 0; i < 5; i++ ) { list1.add ( "List item " + i ) ; } // Show list System.out.println ( "list1 before clear..." ) ; System.out.println ( "list1 size: " + list1.size ( ) ) ; for ( int i = 0; i < list1.size ( ) ; i++ ) { System.out.println ( list1.get ( i ) ) ; } list1.clear ( ) ; // Show list System.out.println ( "list1 after clear..." ) ; System.out.println ( "list1 size: " + list1.size ( ) ) ; for ( int i = 0; i < list1.size ( ) ; i++ ) { System.out.println ( list1.get ( i ) ) ; } } }
public Object clone() - See Also:
Cloneable
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public boolean contains(Object elem) - See Also:
- E, AbstractCollection, List, Collection
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public void ensureCapacity(int minCapacity) - Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public E get(int index) - See Also:
- IndexOutOfBoundsException, AbstractList, List
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[574]Populate list By VentureKing on 2004/05/18 07:52:29 Rate
import java.util.ArrayList; class testArrayList { public static void main ( String [ ] args ) { ArrayList list1 = new ArrayList ( ) ; // Populate list for ( int i = 0; i < 5; i++ ) { list1.add ( "List item " + i ) ; } // Show list for ( int i = 0; i < list1.size ( ) ; i++ ) { System.out.println ( list1.get ( i ) ) ; } } }
public int indexOf(Object elem) - See Also:
Object.equals(Object) , E, AbstractList, List
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public boolean isEmpty() - See Also:
- E, AbstractCollection, List, Collection
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public int lastIndexOf(Object elem) - See Also:
- E, AbstractList, List
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[1966]Example of latIndexOf usage By jertix { at } tiscali { dot } it on 2008/05/15 08:43:47 Rate
package testArrayList; import java.util.ArrayList; public class testlastIndexOf { public static void main ( String [ ] args ) { ArrayList < String > listNames = new ArrayList < String > ( ) ; String tmpName = ""; tmpName="Mark"; listNames.add ( tmpName ) ; // index 0 tmpName="Fred"; listNames.add ( tmpName ) ; // index 1 tmpName="Emily"; listNames.add ( tmpName ) ; // index 2 System.out.println ( "Last item number: " + listNames.lastIndexOf ( tmpName ) ) ; System.out.println ( "Last item value: " + listNames.get ( listNames.lastIndexOf ( tmpName ) ) ) ; // OUTPUT: // Last item number: 2 // Last item value: Emily } }
public E remove(int index) - See Also:
- IndexOutOfBoundsException, AbstractList, List
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[577]Modify list By VentureKing on 2004/06/05 10:57:15 Rate
import java.util.ArrayList; class testArrayList { public static void main ( String [ ] args ) { ArrayList list1 = new ArrayList ( ) ; // Populate list for ( int i = 0; i < 5; i++ ) { list1.add ( "List item " + i ) ; } // Show list System.out.println ( "list1 size: " + list1.size ( ) ) ; for ( int i = 0; i < list1.size ( ) ; i++ ) { System.out.println ( list1.get ( i ) ) ; } list1.set ( 2, "List item 2 ( modified ) " ) ; list1.remove ( 1 ) ; // Show list System.out.println ( "list1 size: " + list1.size ( ) ) ; for ( int i = 0; i < list1.size ( ) ; i++ ) { System.out.println ( list1.get ( i ) ) ; } } }
public boolean remove(Object o) - See Also:
- E, AbstractCollection, List, Collection
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
protected void removeRange(int fromIndex,
int toIndex) - See Also:
- E, AbstractList
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[1146]Example for Protected Method removeRange By mk_rauf { at } yahoo { dot } com on 2004/11/26 06:16:09 Rate
//Example for Protected Method removeRange public class Test extends java.util.ArrayList { public static void main ( String [ ] args ) { Test t= new Test ( ) ; t.add ( "5" ) ; t.add ( "7777" ) ; t.add ( "8888885" ) ; t.add ( "93935" ) ; t.add ( "5333" ) ; t.add ( "466" ) ; System.out.println ( "The size of arraylist ="+t.size ( ) ) ; for ( int i=0;i < t.size ( ) ;i++ ) { System.out.println ( " "+ ( i+1 ) +" ) "+t.get ( i ) ) ; } t.removeRange ( 3,5 ) ; System.out.println ( "The size of arraylist ="+t.size ( ) ) ; for ( int i=0;i < t.size ( ) ;i++ ) { System.out.println ( " "+ ( i+1 ) +" ) "+t.get ( i ) ) ; } } }
public E set(int index,
E element) - See Also:
- IndexOutOfBoundsException, AbstractList, List
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[1436]Extending ArrayList By cgselvan2002 { at } yahoo { dot } com(c { dot } gokulaselvan) on 2005/05/21 01:40:20 Rate
public class Test1 extends java.util.ArrayList { public static void main ( String [ ] args ) { Test t= new Test ( ) ; t.add ( "100" ) ; t.add ( "200" ) ; t.add ( "300" ) ; t.add ( "400" ) ; System.out.println ( "The size of arraylist ="+t.size ( ) ) ; for ( int i=0;i < t.size ( ) ;i++ ) { System.out.println ( " "+ ( i+1 ) +" ) "+t.get ( i ) ) ; } //t.removeRange ( 3,6 ) ; t.set ( 3,"500" ) ; System.out.println ( "The size of arraylist ="+t.size ( ) ) ; for ( int i=0;i < t.size ( ) ;i++ ) { System.out.println ( " "+ ( i+1 ) +" ) "+t.get ( i ) ) ; } } }
public Object set(int index,
Object element) - See Also:
- IndexOutOfBoundsException, AbstractList, List
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[578]Remove item from ArrayList By VentureKing on 2003/12/19 17:13:34 Rate
import java.util.ArrayList; class testArrayList { public static void main ( String [ ] args ) { ArrayList list1 = new ArrayList ( ) ; // Populate list for ( int i = 0; i < 5; i++ ) { list1.add ( "List item " + i ) ; } // Show list System.out.println ( "list1 size: " + list1.size ( ) ) ; for ( int i = 0; i < list1.size ( ) ; i++ ) { System.out.println ( list1.get ( i ) ) ; } list1.set ( 2, "List item 2 ( modified ) " ) ; list1.remove ( 1 ) ; // Show list System.out.println ( "list1 size: " + list1.size ( ) ) ; for ( int i = 0; i < list1.size ( ) ; i++ ) { System.out.println ( list1.get ( i ) ) ; } } }
public int size() - See Also:
- E, AbstractCollection, List, Collection
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[575]Populate, show, clear ArrayList By VentureKing on 2005/08/10 08:25:45 Rate
import java.util.ArrayList; class testArrayList { public static void main ( String [ ] args ) { ArrayList list1 = new ArrayList ( ) ; // Populate list for ( int i = 0; i < 5; i++ ) { list1.add ( "List item " + i ) ; } // Show list System.out.println ( "list1 before clear..." ) ; System.out.println ( "list1 size: " + list1.size ( ) ) ; for ( int i = 0; i < list1.size ( ) ; i++ ) { System.out.println ( list1.get ( i ) ) ; } list1.clear ( ) ; // Show list System.out.println ( "list1 after clear..." ) ; System.out.println ( "list1 size: " + list1.size ( ) ) ; for ( int i = 0; i < list1.size ( ) ; i++ ) { System.out.println ( list1.get ( i ) ) ; } } }
public Object[] toArray() - See Also:
Arrays.asList(Object[]) , E, AbstractCollection, List, Collection
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[111]Convert ArrayList to Array By Anonymous on 2005/04/05 09:08:14 Rate
HashMap [ ] results = ( HashMap [ ] ) al.toArray ( ) ;
public Object[] toArray(Object[] a) - See Also:
- ArrayStoreException, AbstractCollection, List
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[423]More ways to convert ArrayList to Array By uymai on 2004/11/25 00:25:59 Rate
String [ ] result = ( String [ ] ) list.toArray ( new String [ list.size ( ) ] ) ; [475]_ By nylz on 2005/03/01 11:39:23 Rate
String [ ] strList = ( String [ ] ) myArrayList.toArray ( new String [ myArrayList.size ( ) ] ) ; works better, because it ensures the size of the Array. [1125]_ By Khurram Chaudhry on 2004/11/25 00:26:05 Rate
Instead of String [ ] parameters = ( String [ ] ) out.toArray ( ) ; you could try: Object [ ] o = out.toArray ( ) ; String [ ] parameters=new String [ o.length ] ; System.arraycopy ( o,0,parameters,0,parameters.length ) ;
public <T> T[] toArray(T[] a) - See Also:
- ArrayStoreException, E, AbstractCollection, List, Collection
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[1668]Difference between ArrayList and Array By Anonymous on 2005/11/05 15:35:08 Rate
ArrayList is a part of the Collection Framework, it can store any type of objects and only objects and it is growable. Array is collection of same type of data items, it can have array of primitives or objects, and it also has fixed size and can have multi dimensions. [1886]Two ways to convert ArrayList to String[] By Anonymous on 2007/06/05 14:42:46 Rate
ArrayList al = ...; String str [ ] = ( String [ ] ) al.toArray ( new String [ al.size ( ) ] ) ; or String str [ ] = new String [ al.size ( ) ] ; al.toArray ( str ) ;
public void trimToSize() - Geek's Notes:
- Description Add your codes or notes Search More Java Examples
| Popular Tags |