boolean containsE ob xequalsob int size return the number of items in the List

Boolean containse ob xequalsob int size return the

This preview shows page 2 - 5 out of 12 pages.

boolean contains(E ob) x.equals(ob) ) int size() return the number of items in the List boolean isEmpty() return true iff the List is empty E get(int pos) return the item at position pos in the List (error if pos is less than 0 or greater than or equal to size() ) E remove(int pos) remove and return the item at position pos in the List, moving the items originally in positions pos+1 through size()-1 one place to the left to fill in the gap (error if pos is less than 0 or greater than or equal to size() ) Page 2 of 12 Lists 2008/3/27
Image of page 2
The main advantage of a List compared to an array is that whereas the size of an array is fixed when it is created (e.g., int[] A = new int[10] creates an array of integers of size 10, and you cannot store more than 10 integers in that array), the size of a List can change: the size increases by one each time a new item is added (using either version of the add method), and the size decreases by one each time an item is removed (using the remove method). For example, here's some code that reads strings from a file called data.txt and stores them in a List of Strings named words , initialized to be an ArrayList of Strings : If we wanted to store the strings in an array , we'd have to know how many strings there were so that we could create an array large enough to hold all of them. One disadvantage of an List compared to an array is that whereas you can create an array of any size, and then you can fill in any element in that array, a new List always has size zero, and you can never add an object at a position greater than the size. For example, the following code is fine: but this code will cause a runtime exception: Another (small) disadvantage of a List compared to an array is that you can declare an array to hold any type of items, including primitive types (e.g., int , char ), but a List only holds Object s. Thus the following causes a compile-time error: Fortunately, for each primitive type, there is a corresponding "box" class. For example, an Integer is an object that contains one int value. Java has a feature called auto-boxing that automatically converts between int and Integer as appropriate. For example, List<String> words = new ArrayList<String>(); File infile = new File("data.txt"); Scanner sc = new Scanner(infile); while (sc.hasNext()) { String str = sc.next(); words.add(str); } String[] strArray = new String[10]; strArray[5] = "hello"; ! List<String> strList = new ArrayList<String>(); strList.add(5, "hello"); // error! can only add at position 0 List<int> numbers = new ArrayList<int>(); % javac Test.java Test.java:9: unexpected type found : int required: reference List<int> numbers = new ArrayList<int>(); ^ Page 3 of 12 Lists 2008/3/27
Image of page 3
TEST YOURSELF #2 Question 1. Assume that variable words is a List containing k strings, for some integer k greater than or equal to zero. Write code that changes words to contain 2* k strings by adding a new copy of each string right after the old copy. For example, if words is like this before your code executes: ["happy", "birthday", "to", "you"] then after your code executes
Image of page 4
Image of page 5

You've reached the end of your free preview.

Want to read all 12 pages?

  • Spring '08
  • MarvinSolomon
  • Data Structures, numItems, SimpleArrayList

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture