Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Ganesh_JavaSE7_Programming_1z0-804_study_guide.pdf
Скачиваний:
94
Добавлен:
02.02.2015
Размер:
5.88 Mб
Скачать

Chapter 6 Generics and Collections

List<Double> temperatureList = new ArrayList<Double>(Arrays.asList(temperatureArray)); temperatureList.add(32.3);

System.out.println("The new List with an added element is: " + temperatureList);

}

}

It prints the following:

The original array is: [31.1, 30.0, 32.5, 34.9, 33.7, 27.8]

The new List with an added element is: [31.1, 30.0, 32.5, 34.9, 33.7, 27.8, 32.3]

Yes, it works now.

Points to Remember

Here are some interesting facts that might prove useful on your OCPJP exam:

The difference between an ArrayList and ArrayDeque is that you can add an element anywhere in an array list using an index; however, you can add an element only either at the front or end of the array deque. That makes insertion in array deque much efficient than array list; however, navigation in an array deque becomes more expensive than in an array list.

There is one more thing you need to remember about using the List returned from the Arrays.asList() method. Though you cannot add elements to the List returned by

the asList() method, you can modify the List! Also, the modifications you make through the List are reflected in the original array. For example, if you modify the temperature of the first day from 31.1 to 35.2 in the List, the original array gets modified, as shown in Listing 6-33.

Listing 6-33.  ArrayAsList3.java

import java.util.*;

class ArrayAsList3 {

public static void main(String []args) {

Double [] temperatureArray = {31.1, 30.0, 32.5, 34.9, 33.7, 27.8}; System.out.println("The original array is: " + Arrays.toString(temperatureArray));

List<Double> temperatureList = Arrays.asList(temperatureArray); temperatureList.set(0, 35.2);

System.out.println("The modified array is: " + Arrays.toString(temperatureArray));

}

}

It prints the following:

The original array is: [31.1, 30.0, 32.5, 34.9, 33.7, 27.8] The modified array is: [35.2, 30.0, 32.5, 34.9, 33.7, 27.8]

The Arrays class provides only limited functionality and you will often want to use methods in the Collections class. To achieve that, calling the Arrays.asList() method is a useful technique.

200

Chapter 6 Generics and Collections

Remember that you cannot add elements to the List returned by the Arrays.asList() method. But, you can make changes to the elements in the returned List, and the changes made to that List are reflected back in the array.

Question Time! 

1.Predict the output of this program:

import java.util.*;

class UtilitiesTest {

public static void main(String []args) { List<int> intList = new ArrayList<>(); intList.add(10);

intList.add(20);

System.out.println("The list is: " + intList);

}

}

A.It prints the following: The list is: [10, 20].

B.It prints the following: The list is: [20, 10].

C.It results in a compiler error.

D.It results in a runtime exception.

Answer: C.  It results in a compiler error.

(You cannot specify primitive types along with generics, so List<int> needs to be changed to

List<Integer>).

2.Predict the output of this program:

import java.util.*;

class UtilitiesTest {

public static void main(String []args) { List<Integer> intList = new LinkedList<>(); List<Double> dblList = new LinkedList<>();

System.out.println("First type: " + intList.getClass()); System.out.println("Second type:" + dblList.getClass());

}

}

A.It prints the following:

First type: class java.util.LinkedList Second type:class java.util.LinkedList

201

Chapter 6 Generics and Collections

B.It prints the following:

First type: class java.util.LinkedList<Integer> Second type:class java.util.LinkedList<Double>

C.It results in a compiler error.

D.It results in a runtime exception.

Answer: A.  It prints the following:

First type: class java.util.LinkedList Second type:class java.util.LinkedList

(Due to type erasure, after compilation both types are treated as same LinkedList type).

3.Which statement is true with respect to List<?> and List<Object>?

A.Both are same, just two different ways to express a same thing.

B.List<?> is a homogenous list of elements of a same unknown type and List<Object> is a heterogeneous list of elements of different types.

C.List<?> is a heterogeneous list of elements of different types and List<Object> is a homogenous list of elements of a same unknown type.

Answer: B.  List<?> is a homogenous list of elements of a same unknown type and List<Object> is a heterogeneous list of elements of a same unknown type.

4. Predict the output of this program:

import java.io.*;

class LastError<T> { private T lastError; void setError(T t){

lastError = t; System.out.println("LastError: setError");

}

}

class StrLastError<S extends CharSequence> extends LastError<String>{ public StrLastError(S s) {

}

void setError(S s){ System.out.println("StrLastError: setError");

}

}

class Test {

public static void main(String []args) {

StrLastError<String> err = new StrLastError<String>("Error"); err.setError("Last error");

}

}

202

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]