Download Problem 1: 4.5 Question: What is a list? What is a set? What is a map

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Problem 1:
4.5
Question:
What is a list? What is a set? What is a map?
Ans:
List: An ordered collection (also known as a sequence). (From java api
document)
Java Collections Framwork's List interface extends the collection interface
by providing some index-related methods. (From P.110 4.2.4)
Set: A collection that contains no duplicate elements.
More formally, sets contain no pair of elements e1 and e2
such that e1.equals(e2), and at most one null element.
As implied by its name, this interface models the mathematical set
abstraction.
(From java api document)
The Set interface extends the Collection interface by prohibiting
duplicate elements. (From P.113 4.2.5)
Map: An object that maps keys to values. A map cannot contain duplicate
keys; each key can map to at most one value. (From java api
document)
A map is a collection in which each element has two parts:
a unique key part and a value part. The idea behind this definition is
that is a "mapping" from each key to corresponding value. (From
P.115 4.2.6)
Problem 2:
chapter 4 slides
p.54, p. 55
P.54
List<String> myList = new ArrayList<String>();
myList.add ("Chelebiev"); myList.add ("Culbertson");
myList.add ("Curry"); myList.add ("Dominguez");
myList.add ("Driscoll");
System.out.println (myList);
System.out.println (myList.get (2));
myList.set (3, "Amanik");
myList.add (4, "Carson");
myList.remove (5);
System.out.println ("Carson is at index " +
myList.indexOf ("Carson"));
for (String name: myList)
if (name.charAt (0) == 'C')
System.out.print (name + " ");
p.55
List<String> myList = new LinkedList<String>();
myList.add ("Chelebiev"); myList.add ("Culbertson");
myList.add ("Curry"); myList.add ("Dominguez");
myList.add ("Driscoll");
System.out.println (myList);
System.out.println (myList.get (2));
myList.set (3, "Amanik");
myList.add (4, "Carson");
myList.remove (5);
System.out.println ("Carson is at index " +
myList.indexOf ("Carson"));
for(String name: myList)
if(name.charAt (0) == 'C')
System.out.print (name + " ");
Run the two programs above and show the outputs, respectively. Also, what is
the difference between the two statements below?
List<String> myList = new ArrayList<String>();
List<String> myList = new LinkedList<String>();
Hint: You should import some packages or classes, such as
import java.util.List;
Ans:
Solution I:
You should import some class in java package with two ways.
Example I:
import java.util.List;
import java.util.ArrayList;
import java.util.LinkedList;
Example II:
import java.util.*;
Note:
import java.util.*; It means import all class in this package.
Solution II:
You can directly add the package name in the class you want to use.
Ex:
java.util.List<String> myList = new java.util.ArrayList<String>();
java.util.List<String> myList = new java.util.LinkedList<String>();
Then, the two programs can be executed correctly.
The output should like follows:
Garica[Chelebiev, Culbertson, Curry, Dominguez, Driscoll]
Curry
Carson is at index 4
Chelebiev Culbertson Curry Carson
What are the differences:
List is a interface. The ArrayList and the LinkedList is its implementations.
There is no difference to using the method of the List interface. But actually,
the operations of each method are totally different between these two classes.