250 likes | 404 Views
Exposure Java. Chapter 20 Slides. Using the ArrayList Class. PowerPoint Presentation created by: Mr. John L. M. Schram. From Materials Created by Mr. Leon Schram. Introduction. Chapter XIII introduced the array data structure.
E N D
Exposure Java Chapter 20 Slides Using the ArrayList Class PowerPoint Presentation created by: Mr. John L. M. Schram From Materials Created by Mr. Leon Schram
Introduction Chapter XIII introduced the array data structure. Historically, the array was the first data structure used by programming languages. With arrays you learned to store multiple values in a single variable. You also learned that access to any individual array element is possible with a special index operator.
Static Array Limitations The actual introduction of the ArrayList class needs to wait for a brief review of the Java static array. // Java2001.java // This program fills a static integer array with 10 random numbers. import java.util.Random; import java.io.*; public class Java2001 { public static void main (String args[]) { System.out.println("\nJava2001.java\n"); int list[] = new int[10]; Random random = new Random(12345); for (int k = 0; k < 10; k++) list[k] = random.nextInt(900) + 100; for (int k = 0; k < 10; k++) System.out.print(list[k] + " "); System.out.println("\n\n"); } } Java2001.java Output Java2001.java 851 680 241 928 455 284 575 802 701 889
// Java2002.java // This program enters the array size during program execution. import java.util.Random; import java.io.*; public class Java2002 { public static void main (String args[]) throws IOException { System.out.println("\nJava2002.java\n"); BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter array size ===>> "); int size = Integer.parseInt(input.readLine()); System.out.println(); int list[] = new int[size]; Random random = new Random(12345); for (int k = 0; k < size; k++) list[k] = random.nextInt(900) + 100; for (int k = 0; k < size; k++) System.out.print(list[k] + " "); System.out.println("\n\n"); } }
Java2002.java Output #1 Java2002.java Enter array size ===>> 20 851 680 241 928 455 284 575 802 701 889 717 142 890 206 312 584 687 803 432 775 Java2002.java Output #2 Java2002.java Enter array size ===>> 100 851 680 241 928 455 284 575 802 701 889 717 142 890 206 312 584 687 803 432 775 201 851 992 116 228 481 525 843 171 739 229 297 301 425 903 855 852 931 710 608 496 182 799 558 318 980 712 791 750 628 274 944 154 608 180 199 258 955 794 697 694 487 353 218 783 599 970 473 490 719 876 921 218 656 488 226 649 392 552 575 103 500 832 664 175 644 709 555 198 116 587 539 653 667 551 541 515 966 274 377
// Java2003.java This program attempts to resize the list array. // It does resize during program execution, but it destroys the existing array. public class Java2003 { public static void main (String args[]) throws IOException { System.out.println("\nJava2003.java\n"); BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter array size ===>> "); int size = Integer.parseInt(input.readLine()); System.out.println(); int list[] = new int[size]; Random random = new Random(12345); for (int k = 0; k < size; k++) list[k] = random.nextInt(900) + 100; for (int k = 0; k < size; k++) System.out.print(list[k] + " "); System.out.println(); System.out.print("Enter array size ===>> "); size = Integer.parseInt(input.readLine()); System.out.println(); list = new int[size]; for (int k = 11; k < size; k++) list[k] = random.nextInt(900) + 100; for (int k = 0; k < size; k++) System.out.print(list[k] + " "); System.out.println("\n\n"); } }
Java2003.java Output • Java2003.java • Enter array size ===>> 10 • 680 241 928 455 284 575 802 701 889 • Enter array size ===>> 100 • 0 0 0 0 0 0 0 0 0 0 0 717 142 890 206 312 584 687 803 432 775 201 851 992 116 228 481 525 843 171 739 229 297 301 425 903 855 852 931 710 608 496 182 799 558 318 980 712 791 750 628 274 944 154 608 180 199 258 955 794 697 694 487 353 218 783 599 970 473 490 719 876 921 218 656 488 226 649 392 552 575 103 500 832 664 175 644 709 555 198 116 587 539 653 667 551 541 515 966 274
Static ArrayLists and Resizing Java static arrays cannot be altered after they are constructed. It is possible to use the same array identifier to construct a different object with a different size. Any of the values stored in the original object will be lost.
// Java2004.java // This program introduces the <ArrayList> class with the <add> method // to add additional elements to the end of the list. // Note, that it is possible to display ArrayList elements directly. import java.util.ArrayList; public class Java2004 { public static void main(String args[]) { System.out.println(); System.out.println("Java2004.java\n"); ArrayList names = new ArrayList(); names.add("Isolde"); names.add("John"); names.add("Greg"); names.add("Maria"); names.add(new String("Heidi")); System.out.println("names contains " + names); System.out.println(); } } Java2004.java Output Java2004.java names contains [Isolde, John, Greg, Maria, Heidi]
ArrayList Access ArrayList objects cannot be accessed with an index [ ] operator, like a Java static array. All access is performed with ArrayList methods.
ArrayList Method add names.add("Tom"); The add method allocates space for the newly enlarged array and then stores the new array element at the end of the ArrayList object.
Displaying ArrayList Elements ArrayList elements can be accessed with various methods. It is possible to display all the elements inside square brackets, separated by commas by using the print method. System.out.println(names); [Isolde, John, Greg, Maria, Heidi]
// Java2005.java // This program uses the <size>method to determine the number of elements // in an <ArrayList object. import java.util.ArrayList; public class Java2005 { public static void main(String args[]) { System.out.println(); System.out.println("Java2005.java\n"); ArrayList names = new ArrayList(); names.add("Isolde"); names.add("John"); names.add("Greg"); names.add("Maria"); names.add(new String("Heidi")); System.out.println("names contains " + names); System.out.println(); System.out.println("There are " + names.size() + " elements in the names object."); System.out.println(); } } Java2005.java Output Java2005.java names contains [Isolde, John, Greg, Maria, Heidi] There are 5 elements in the names object.
ArrayList Method size int count = names.size(); The size method returns the number of elements of the ArrayList object names.
// Java2006.java // This program shows how to access specified elements in an <ArrayList> object // with the <get> method. import java.util.ArrayList; public class Java2006 { public static void main(String args[]) { System.out.println(); System.out.println("Java2006.java\n"); ArrayList names = new ArrayList(); names.add("Isolde"); names.add("John"); names.add("Greg"); names.add("Maria"); names.add(new String("Heidi")); System.out.println(); for (int k = 0; k < names.size(); k++) System.out.println(names.get(k)); System.out.println(); for (int k = names.size()-1; k >= 0; k--) System.out.println(names.get(k)); } } Java2006.java Output Java2006.java Isolde John Greg Maria Heidi Heidi Maria Greg John Isolde
ArrayList Method get System.out.println(names.get(3)); The get method accesses a specified array element. The parameter of the get method is the index of the ArrayList object and starts at 0. Any attempt to access an element at a non-existing index results in an IndexOutOfBoundsException error.
// Java2007.java // This program demonstrates the <set> method of the <ArrayList> class, which // replaces existing elements with a new object. import java.util.ArrayList; public class Java2007 { public static void main(String args[]) { System.out.println("Java2007.java\n"); ArrayList names = new ArrayList(); names.add("Isolde"); names.add("John"); names.add("Greg"); names.add("Maria"); names.add("Heidi"); System.out.println("names contains " + names); System.out.println(); names.set(1,"Jessica"); names.set(2,"Anthony"); names.set(3,"Haley"); names.set(4,"Alec"); System.out.println("names contains " + names); } } Java2007.java Output Java2007.java names contains [Isolde, John, Greg, Maria, Heidi] names contains [Isolde, Jessica, Anthony, Haley, Alec]
ArrayList Method set names.set(4,"Tom"); The set method uses the first parameter as the index location to find an array element and then replaces it with the value of the second set parameter. You will get an error if you try to access an index location, which has not been allocated yet.
// Java2008.java // This program demonstrates the <remove> method of the <ArrayList> class to // delete a specified list element. import java.util.ArrayList; public class Java2008 { public static void main(String args[]) { System.out.println(); System.out.println("Java2008.java\n"); ArrayList names = new ArrayList(); names.add("Isolde"); names.add("John"); names.add("Greg"); names.add("Maria"); names.add("Heidi"); System.out.println("names contains " + names); System.out.println(); names.remove(2); System.out.println("names contains " + names); System.out.println(); names.remove(3); System.out.println("names contains " + names); System.out.println(); } } Java2008.java Output Java2008.java names contains [Isolde, John, Greg, Maria, Heidi] names contains [Isolde, John, Maria, Heidi] names contains [Isolde, John, Maria]
ArrayList Method remove names.remove(3); The remove method removes the array element at the index location of its parameter and decreases the object size by one array element. You will get an error if you try to access an index location, which does not exist.
// Java2009.java // This program demonstrates how to use the <add> method of the <ArrayList> class to // insert new elements at a specified location. import java.util.ArrayList; public class Java2009 { public static void main(String args[]) { System.out.println("Java2009.java\n"); ArrayList names = new ArrayList(); names.add("Isolde"); names.add("John"); names.add("Greg"); names.add("Maria"); names.add("Heidi"); System.out.println("names contains " + names); System.out.println(); names.add(2,"Jessica"); System.out.println("names contains " + names); System.out.println(); names.add(3,"Anthony"); System.out.println("names contains " + names); } } Java2009.java Output Java2009.java names contains [Isolde, John, Greg, Maria, Heidi] names contains [Isolde, John, Jessica, Greg, Maria, Heidi] names contains [Isolde, John, Jessica, Anthony, Greg, Maria, Heidi]
ArrayList Method add(2nd Overloaded method) names.add(3,"Kathy"); The overloaded add(3,"Kathy") method adds or rather inserts a new array element at the indicated index. As always, you will get an error if you try to access an index location, which does not exist.
// Java2010.java // This program demonstrates how to use the <clear> method to remove all the // array elements and the <isEmpty> method to check if emptyness is true. import java.util.ArrayList; public class Java2010 { public static void main(String args[]) { System.out.println("Java2010.java\n"); ArrayList names = new ArrayList(); names.add("Isolde"); names.add("John"); names.add("Greg"); names.add("Maria"); names.add("Heidi"); System.out.println("names contains " + names); System.out.println("Empty list is " + names.isEmpty()); names.clear(); System.out.println(); System.out.println("names contains " + names); System.out.println("Empty list is " + names.isEmpty()); } } Java2010.java Output Java2010.java names contains [Isolde, John, Greg, Maria, Heidi] Empty list is false names contains [] Empty list is true
ArrayList Method add(2nd Overloaded method) names.add(3,"Kathy"); The overloaded add(3,"Kathy") method adds or rather inserts a new array element at the indicated index. As always, you will get an error if you try to access an index location, which does not exist.
Java1506.java:13: cannot resolve symbol symbol : class Widget location: class Java1506 Widget w = new Widget(10); ^ Java1506.java:13: cannot resolve symbol symbol : class Widget location: class Java1506 Widget w = new Widget(10); ^ Java1506.java:19: cannot resolve symbol symbol : class Aardvark location: class Java1506 Aardvark a = new Aardvark(20); ^ Java1506.java:19: cannot resolve symbol symbol : class Aardvark location: class Java1506 Aardvark a = new Aardvark(20); ^ Java1506.java:25: cannot resolve symbol symbol : class Mango location: class Java1506 Mango m = new Mango(25); ^ Java1506.java:25: cannot resolve symbol symbol : class Mango location: class Java1506 Mango m = new Mango(25); ^ 6 errors Java1506.java Output when trying to compile the file Java1506.java. Since these files are all in different folders, this cannot work without projects.