CSE 1341 Composition Array List 2012 Remember Arrays
CSE 1341 Composition Array. List 2012
Remember Arrays? 2012
Recap array a a[0] a[1] a[2] a[3] . . . a[10] . . . • An array has a name (here a) • The elements can be accessed using the index. • This length of the array is fixed after creation. 2012
Let’s Code this… Farmer Pig -pigs 1 * -name : String +Pig(String, IDCollar) +oink() : String +create. Pigs(): void 1 1 collar 1 IDCollar -collars -id: int * 2012 +IDCollar(int) +get. ID() : int +set. ID(int) : void
Collections Overview • A collection is a data structure—actually, an object— that can hold references to other objects. – Usually, collections contain references to objects that are all of the same type. • Package java. util. • Some of the interfaces of the collections framework: 2012
Java Collection Hierarchy 2012
Lists • A List (sometimes called a sequence) is a Collection that can contain duplicate elements. • List indices are zero based. • Interface List is implemented by several classes, including Array. List, Linked. List and Vector. • Class Array. List and Vector are resizable-array implementations of List. • Inserting an element between existing elements of an Array. List or Vector is an inefficient operation. • A Linked. List enables efficient insertion (or removal) of elements in the middle of a collection. 2012
Array. List • Collections provide efficient methods that organize, store and retrieve your data without requiring knowledge of how the data is being stored. • The collection class Array. List<T> (from package java. util) can dynamically change its size to accommodate more elements. • The T is a placeholder—when declaring a new Array. List, replace it with the type of elements that you want the Array. List to hold. • This is similar to specifying the type when declaring an array, except that only nonprimitive types can be used with these collection classes. • List method adds an item to the end of a list. • List method size returns the number of elements. • List method get retrieves an individual element’s value from the specified index. 2012
Creating an Array. List<Shoe> my. List = new Array. List<Shoe>(); Initial size is 0, and it grows as you add items. Can contain any type of object. my. List. add(new Shoe()); 2012
Array } } 2012
Retrieving Objects from an Array. List Shoe my. Shoe = my. List. get(0); Index location is 0. . n, where n is the size of the list minus one. //RETRIEVE AN OBJECT: my. List. get(int); //TOTAL OBJECTS: my. List. size(); 2012
2012
Array. List Example Program 2012
2012
Refactor with Array. List… Farmer Pig pigs 1 10 -name : String +Pig(String, IDCollar) +oink() : String +create. Pig() 1 1 collar 1 IDCollar collars * 2012 -id: int +IDCollar(int) +get. ID() : int -set. ID(int) : void
String split method public class Tester { public static void main(String[] args) { String s = "A B CD"; String[] strings = s. split(" "); for(String sa : strings) System. out. println(sa); } } 2012
Pig. Pen pigs 1 +Pig. Pen()+create. Pigs () : void 2012 * Pig -name : String +Pig(String) +oink() : String
- Slides: 17