Ο ΔΥΝΑΜΙΚΟΣ ΠΙΝΑΚΑΣ ARRAYLIST | Boolean Data Type | String (Computer Science)

Please download to get full document.

View again

All materials on our website are shared by users. If you have any questions about copyright issues, please report us to resolve them. We are always happy to assist you.
 3
 
  Λυµένα Θέµατα και Ασκήσεις κ.λ.π ΘΕΩΡΙΑ-ΜΕΘΟ∆ΟΛΟΓΙΑ Tιτλος: ∆υναµικοί Πίνακες ArrayLists Τρόποι χειρισµού ∆υναµικών Πινάκων Θεωρία //χρήση της ArrayList import java.util.ArrayList; class testArrayList { public static void main ( String [ ] args ) ArrayList list1 = new ArrayList () ; // προσθήκη στοιχείων στη λίστα for ( int i = 5; i 0; i-- ) { list1.add ( 0, List item + i ) ; } // εκτύπωση λίστας System.out.println ( list1 size: + list1.size ( ) for ( int i = 0; i list1.size ( ) ; i++
Share
Transcript
    Λυ µ ένα   Θέ µ ατα   και   Ασκήσεις   κ . λ . π   ΣΟΛΩΜΟΥ 29  ΑΘΗΝΑ    210.38.22.157 – 495www.arnos.gr – e-mail :info@arnos.gr   ΟΛΟΚΛΗΡΩΜΕΝΗ   ΣΤΗΡΙΞΗ   ΦΟΙΤΗΤΩΝ   1 ΘΕΩΡΙΑ - ΜΕΘΟ∆ΟΛΟΓΙΑ  T ιτλος : ∆υνα µ ικοί   Πίνακες ArrayLists Τρόποι   χειρισ µ ού   ∆υνα µ ικών   Πινάκων   Θεωρία   // χρήση   της ArrayListimport java.util.ArrayList;class testArrayList {public static void main ( String [ ] args ) {ArrayList list1 = new ArrayList () ;   // προσθήκη   στοιχείων   στη   λίστα  for ( int i = 5; i > 0; i-- ) {list1.add ( 0, List item + i ) ;}// εκτύπωση   λίστας  System.out.println ( list1 size: + list1.size ( ) ) ;for ( int i = 0; i < list1.size ( ) ; i++ ) {System.out.println ( list1.get ( i ) ) ;}}} // ΠΩΣ    ΝΑ    ΧΡΗΣΙΜΟΠΟΙΕΙΤΕ   ΤΙΣ    ΛΙΣΤΕΣ   import java.util.*;public class UseArrayList {public static void main ( String [ ] args ) {//How to Use ArrayList//BeginArrayList arraylist = new ArrayList ( ) ;Stringarr [ ] = { india , , three ,null, new Integer ( 1 ) } ;//Different ways to Add Elements to the arraylistfor ( 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 arraylistSystem.out.println ( The elements of arraylist are ) ;for ( int i=0;i < arraylist.size ( ) ;i++ ){System.out.println (     Λυ µ ένα   Θέ µ ατα   και   Ασκήσεις   κ . λ . π   ΣΟΛΩΜΟΥ 29  ΑΘΗΝΑ    210.38.22.157 – 495www.arnos.gr – e-mail :info@arnos.gr   ΟΛΟΚΛΗΡΩΜΕΝΗ   ΣΤΗΡΙΞΗ   ΦΟΙΤΗΤΩΝ   2 + ( 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 onearraylist.set ( 1, J2EE Programmer ) ;//Converts arraylist to arraySystem.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 notSystem.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 4System.out.println ( IndexOf \ null\ value= +arraylist.indexOf ( null ) ) ;//Gives the Last Occurance of the ObjectSystem.out.println ( LastIndexOf \ new Float ( 3.5 ) \ value= +arraylist.lastIndexOf ( new Float ( 3.5 ) ) ) ;//Remove the elements from the arraylistarraylist.remove ( 2 ) ;System.out.println ( The size of arraylist= +arraylist.size ( ) ) ;//arraylist.removeRange ( ) ; See removeRange method in thesame sitearraylist.clear ( ) ;System.out.println ( The size of arraylist= +arraylist.size ( ) ) ;//End of ArrayList}}// ΠΑΡΑ∆ΕΙΓΜΑ     ΛΙΣΤΑΣ    ΜΕ    ΑΝΤΙΚΕΙΜΕΝΑ    /*** The ArrayList implements a growable array.* Insertions are always done at the end.*/public class ArrayList<AnyType> extends AbstractCollection<AnyType>implements List<AnyType>{/*** Construct an empty ArrayList.*/public ArrayList( )    Λυ µ ένα   Θέ µ ατα   και   Ασκήσεις   κ . λ . π   ΣΟΛΩΜΟΥ 29  ΑΘΗΝΑ    210.38.22.157 – 495www.arnos.gr – e-mail :info@arnos.gr   ΟΛΟΚΛΗΡΩΜΕΝΗ   ΣΤΗΡΙΞΗ   ΦΟΙΤΗΤΩΝ   3 {clear( );}/*** Construct an ArrayList with same items as another Collection.*/public ArrayList( Collection<? extends AnyType> other ){clear( );for( AnyType obj : other )add( obj );}/*** Returns the number of items in this collection.   * @return the number of items in this collection.*/public int size( ){return theSize;}/*** Returns the item at position idx.* @param idx the index to search in.* @throws ArrayIndexOutOfBoundsException if index is out ofrange.*/public AnyType get( int idx ){if( idx < 0 || idx >= size( ) )throw new ArrayIndexOutOfBoundsException( Index + idx+ ; size + size( ) );return theItems[ idx ];}/*** Changes the item at position idx.* @param idx the index to change.* @param newVal the new value.* @return the old value.* @throws ArrayIndexOutOfBoundsException if index is out ofrange.*/public AnyType set( int idx, AnyType newVal ){if( idx < 0 || idx >= size( ) )throw new ArrayIndexOutOfBoundsException( Index + idx+ ; size + size( ) );AnyType old = theItems[ idx ];theItems[ idx ] = newVal;return old;}/*** Tests if some item is in this collection.* @param x any object.* @return true if this collection contains an item equal to x.*/public boolean contains( Object x ){return findPos( x ) != NOT_FOUND;}    Λυ µ ένα   Θέ µ ατα   και   Ασκήσεις   κ . λ . π   ΣΟΛΩΜΟΥ 29  ΑΘΗΝΑ    210.38.22.157 – 495www.arnos.gr – e-mail :info@arnos.gr   ΟΛΟΚΛΗΡΩΜΕΝΗ   ΣΤΗΡΙΞΗ   ΦΟΙΤΗΤΩΝ   4  /*** Returns the position of first item matching x in thiscollection,* or NOT_FOUND if not found.* @param x any object.* @return the position of first item matching x in thiscollection,* or NOT_FOUND if not found.*/private int findPos( Object x ){for( int i = 0; i < size( ); i++ )if( x == null ){   if( theItems[ i ] == null )return i;}else if( x.equals( theItems[ i ] ) )return i;return NOT_FOUND;}/*** Adds an item to this collection, at the end.* @param x any object.* @return true.*/public boolean add( AnyType x ){if( theItems.length == size( ) ){AnyType [ ] old = theItems;theItems = (AnyType []) new Object[ theItems.length * 2 +1 ];for( int i = 0; i < size( ); i++ )theItems[ i ] = old[ i ];}theItems[ theSize++ ] = x;modCount++;return true;}/*** Removes an item from this collection.* @param x any object.* @return true if this item was removed from the collection.*/public boolean remove( Object x ){int pos = findPos( x );if( pos == NOT_FOUND )return false;else{remove( pos );return true;}}/**
Related Search
We Need Your Support
Thank you for visiting our website and your interest in our free products and services. We are nonprofit website to share and download documents. To the running of this website, we need your help to support us.

Thanks to everyone for your continued support.

No, Thanks
SAVE OUR EARTH

We need your sign to support Project to invent "SMART AND CONTROLLABLE REFLECTIVE BALLOONS" to cover the Sun and Save Our Earth.

More details...

Sign Now!

We are very appreciated for your Prompt Action!

x