Unit 3 Test Friday Review libraries and packages

Unit 3 Test: Friday

Review: libraries and packages • In Java, a library is a group of packages. • A package is a group of classes that belong together. • For example, the java. lang package (lang is a package inside the java library) is the most used package, and provides classes that are fundamental to the language, such as String and Object. • Another very useful package is java. util. • It is possible to create your own package, although that is not part of the AP curriculum.

What if a method has no visibility modifier? • Sometimes you will see a method that is neither private nor public, such as: void display. Num() { System. out. println(x); } • This is called a package protected method • This method can only be called by: 1) Other methods in the same class 2) Other classes in the same package • This method cannot be called by any subclasses.


The Array. List Class • The Array. List class is part of the java. util package • An Array. List is like a standard array, except that it automatically grows and shrinks as elements are added or deleted (so, there are never ANY empty elements in an Array. List). • Items can be inserted or removed with a single method call • It stores references to the Object class, which allows it to store ANY kind of object – so, you can store different types in the same array

• However, this means that you cannot store primitive types (int, double, char, boolean) in an Array. List. Instead, you would store their Wrapper Class equivalents: – Integer – Double – Character – Boolean

• When using an Array. List, you might get this compiler message: filename uses unchecked or unsafe operations. Note: Recompile with -Xlint: unchecked for details. • This is just a warning, not an actual error. You can ignore it.

Array. List Syntax • Import java. util. Array. List • Array. List is a class, so you must instantiate an object of it • If you want to add an object (such as a String) to an Array. List, there is no special syntax. However, if you want to add a “primitive type” (such as int, double, char), you must add using a wrapper class. Commonly used Array. List methods: – add (obj) // adds obj at the end of the list – add (index, obj) // adds obj at the specified index – set (index, obj) // replaces the value at the specified index with obj – get (index) // returns the object at the specified index – index. Of(obj) // finds the index of the specified obj – remove (index) // deletes the object at the specified index – size ( ) // returns the size of the Array. List • Demo: Array. List. Demo

Array. List x = new Array. List(); x. add(“C”); x. add(“A”); x. add(1, “T”); x. remove(0); x. add(0, “M”); x. remove(1); x. set(1, “U”) x. add(“D”); System. out. println(x); // what would display?

• What if you attempt to declare the size of an Array. List? • Example: Array. List numbers = new Array. List(17); System. out. println(numbers. size( )); • Even though this would compile with no errors, the 17 is ignored. • 0 would be displayed.

Unboxing • “Unboxing” means taking an Integer object and assigning its value to a primitive int. • This is done using the. int. Value( ) method. • Example; Integer z = new Integer(7); int y = z. int. Value( ); // box // unbox • This is what we saw in the demo, when accessing an integer that is stored in an Array. List. (See Array. List. Demo. )

• Polymorphism is commonly seen when using Array. List. • Array. List is a class that implements the List interface. The List interface is a blueprint for its “implementor” classes. • There is another implementor of the List interface, called Linked. List, that you do not have to know about, but it helps to be aware of it. • Linked. List is similar to Array. List. It “performs better” than Array. List when using the add and remove methods, but worse than Array. List when using the get and set methods. • So, if you plan on inserting/deleting a lot of elements from a list of objects, Linked. List is preferable. If you just plan on retrieving elements from a list, Array. List is better.

• Unlike Linked. List, you do need to be aware of the List interface. Often, you will see Array. Lists declared this way: List x; • And then, later on: x = new Array. List(); • Alternatively, you might see this: List x = new Array. List(); • Or, a later line of code might want to change x to be a Linked. List, like this: x = new Linked. List(); • If x had been declared as an Array. List originally, this would be a problem. You would have to go back and change a lot of code in order to do this. Using polymorphism here will save you a lot of time. • Note: we learned previously that an interface cannot be instantiated. In this example, we are not instantiating List; we are actually instantiating Array. List. • Remember that since List is an interface, its methods are empty. They don’t actually do anything. The whole point of using List is that you can easily switch back and forth from Array. List to Linked. List by using polymorphism.

Restricting the values in an Arraylist • By default, an Array. List can store any type of Object. • You can, if you wish, restrict the type of thing that an Array. List can hold, by using the < > symbols. This is called using generics. • Examples: 1) Array. List<Person> z = new Array. List<Person>(); 2) Array. List<Integer> x = new Array. List<Integer>(); 3) List<String> a; a = new Array. List<String>();

Why use generics? • Simply put, using generics will make your code more “stable” by catching more errors at compile time rather than at run-time. • Compiler errors are usually easier to fix than runtime errors. • When the compiler knows that an Array. List is restricted to holding Strings, for example, it can check your code before running the program to find any errors that specifically involve using Strings. • Another benefit of using generics in an Array. List is that it eliminates the need for type casting. • Example: Array. List<Integer> nums = new Array. List<Integer>(); nums. add(23); nums. add(11); // no need for “new Integer (11)” etc

• Another example: public class Sample { private Array. List<Comparable> x; // more code here } • In this class, a private instance variable is created – it is an Arraylist. • But note that it will NOT hold objects of the Comparable interface, because that is not valid. An interface cannot be instantiated. • Instead, it will hold objects that implement the Comparable interface.



Assignment (Names. Array) 1) Ask the user how many names they will enter. 2) Then ask for those names, storing them in an Array. List. Display the list. 3) Ask the user to choose one of the names. Delete this person from the list. Display the list. 4) Insert your own name so that it’s the 2 nd name in the list. Display the list. 5) Then add the name “Mike Gordon” to the end of the list. Display the list. 6) Then, ask the user to choose another name to delete, as well as what name will replace it. Replace the name. Display the list. 7) Then, ask how old each person on the list is (ex: “How old is Mike Gordon? ”). Insert each person’s age immediately after that person’s name in the List. Display the list.
- Slides: 19