/* Copyright (c) SEMM NL All rights reserved.
Author : Paul Hamaker. Part of JavaLessons.com
This code is for educational purposes only. Use at own risk.*/

1. import java.awt.event.* ; import java.awt.* ; 

2. import java.applet.*; 

3. import java.util.* ; 

4.  

5. public class Listed 

6.   extends Applet 

7.   implements ActionListener 

8.

9.   ArrayList smartarray ; 

10. //  ArrayList<String> smartarray ; 

11.    

12.   public void actionPerformed ( ActionEvent ev ) 

13.   { 

14.     if ( smartarray == null

15.       smartarray = new ArrayList ( 90 ) ; 

16. //     smartarray = new ArrayList<String> ( 90 ) ; 

17.     smartarray.add ( "Un" ); 

18.     smartarray.add ( "delante" ); 

19.     smartarray.add ( 1, "pasito" ); 

20.  

21.     int pos = smartarray.indexOf ( "delante" ) ; 

22.     if ( pos != -1

23.       smartarray.set ( pos, "atras" ); 

24.  

25.     Object ob = smartarray.get ( pos ) ; 

26. //    String str = smartarray.get ( pos ) ; 

27.     String str = "unknown"

28.     if ( ob != null && 

29.           ob instanceof String

30.       str = (String) ob ; 

31.  

32.     ta.append ( str + " is element #" + pos + "\n" ) ; 

33.  

34.     ListIterator iter = smartarray.listIterator ( ); 

35. //    ListIterator<String> iter = smartarray.listIterator ( ); 

36.     while ( iter.hasNext ( )  ) 

37.     { 

38.       ta.append ( iter.nextIndex( ) + "\t" ); 

39.       ta.append ( (String)iter.next( ) + '\n' ) ; 

40.     } 

41.      ta.append ( "And back :\n" ) ; 

42.  

43.     while ( iter.hasPrevious ( )  ) 

44.     { 

45.       ta.append ( iter.previousIndex( ) + "\t" ); 

46.       ta.append ( (String)iter.previous( ) + '\n' ) ; 

47.     } 

48.     Collections.sort ( smartarray ); 

49.     pos = Collections.binarySearch ( smartarray, "pasito" ); 

50.  

51.     Collections.sort 

52.     ( smartarray, Collections.reverseOrder( )  ); 

53.  

54.     boolean suxes = smartarray.remove ( "pasito" ) ; 

55.  

56.     smartarray.clear( ); 

57.   } 

58.   public void init() 

59.   { 

60.     butt.addActionListener ( this ) ; 

61.     setBackground ( Color.pink ) ; 

62.  

63.     add ( butt ) ; 

64.     add ( ta ) ; 

65.     validate () ; 

66.   } 

67.   TextArea ta = new TextArea ( 10, 30 ) ; 

68.   Button butt = new Button ( "Do it" ) ; 

69.

70. /*===================== 

71.   int [ ] arrayOfInts = { 4556, 3, 6969, 77 } ; 

72.   java.util.Arrays 

73.  

74.   Arrays.sort ( arrayOfInts ); 

75.  

76.   int pos = Arrays.binarySearch ( arrayOfInts, 6969 ); 

77.  

78.   Arrays.sort ( arrayOfStrings ); 

79.   pos = Arrays.binarySearch ( arrayOfStrings, "AllThatIs" ); 

80. */