Java, Ch. 22- Lists, Stacks, Queues, and Priority Queues

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

What is Deque short for?

"Double-ended queue". Usually Deque is pronounced as "deck". The Deque interface extends Queue with additional methods for inserting and removing elements from both ends of the queue.

What methods are defined in the List interface?

+add(index: int, element: Object): boolean +addAll(index: int, c: Collection<? extends E>): boolean +get(index: int): E +indexOf(element: Object): int +lastIndexOf(element: Object): int +listIterator(): ListIterator<E> +listIterator(startIndex: int): ListIterator<E> +remove(index: int): E +set(index: int, element: Object): Object +subList(fromIndex: int, toIndex: int): List<E>

List the methods in the Collection interface in UML (Unified Modeling Language) format.

+add(o: E): boolean +addAll(c: Collection<? extends E>): boolean +clear(): void +contains(o: Object) : boolean +containsAll(c: Collection<?>): boolean +equals(: object): boolean +hashCode(): int +isEmpty(): boolean +remove(o: Object): boolean +removeAll(c: Collection<?>): boolean +retainAll(c: Collection<?>): boolean +size(): int +toArray(): Object[ ]

Explain the addAll, removeAll, and retainAll methods from the Collection interface.

+addAll(c: Collection<? extends E>):boolean -this method accepts a collection argument (i.e. ArrayList, HashSet, Vector, etc..) Of course <? extends E> is a bounded wildcard. The return value is true if the collection is change as a result of the operation. Think of this method as a union. i.e. A U B are the element in A or in B. +removeAll(c: Collection<?>): boolean -Think of this method as the different operator... A - B is equivalent to A and B complement. +retainAll(c:Collection<?>): boolean -Think of this method as a set intersection. The elements in A AND B.

Define all the static collection methods in the java.util.Collections class.

+max(c: Collection): Object +max(c: Collection, c: Comparator): Object +min(c: Collection): Object +min(c: Collection, c: Comparator): Object +disjoint(c1: Collection, c2: Collection): boolean +frequency(c: Collection, o: Object): int

Define all the static list methods in the java.util.Collections class.

+sort(list: List): void +sort(list: List, c: Comparator): void +binarySearch(list: List, key: Object): int +binarySearch(list: List, key: Object, c: Comparator): int +reverse(list: List): void +reverseOrder(): Comparator +shuffle(list: List): void +shuffle(list: List, rmd: Random): void +copy(des: Listsrc: List): void +nCopies(n: int, o: Object): List +fill(list: List, o: Object): void

What is the Java Virtual Machine (JVM)?

A Java virtual machine (JVM) is a process virtual machine that can execute Java bytecode. It is the code execution component of the Java platform. Sun Microsystems has stated that there are over 5.5 billion JVM-enabled devices.

What is a collection?

A container for storing a collection of elements. There are three different kinds of collections.

What is a map?

A container for storing key/value pairs. Maps are efficient data structures for quickly searching an element using a key.

What is the difference between a List and an Array?

A list can grow and shrink dynamically. Once it is created, an array is fixed. If your application does not require the insertion or deletion of elements, an array is the most efficient data structure.

Which methods are in ArrayList but not in LinkedList?

All methods in ArrayList are also in LinkedList except for trim().

Which java data structure is not cloneable?

All the concrete classes in the Java Collections Framework implement the java.lang.Cloneable iand java.io.Serializable interfaces except that java.util.PriorityQueue does not implement the Cloneable interface. Thus, all instances except priority queues can be clone and all instances can be serialized.

What is a major key to developing high-performing software?

Choosing the best data structures and algorithms for a particular task.

Which method can you use to sort a list? Which method can you use to sort an array of String objects?

Collections.sort(list: List) to sort a list. Arrays.sort(Object[]) to sort an array of String objects.

Differentiate between the Comparable interface and the Comparator interface.

Comparable is used to compare the objects of the class that implement Comparable. Comparator can be used to compare the objects of a class that doesn't implement Comparable. In addition, the Comparator interface is generic.

What is the Comparator Interface?

Comparator can be used to compare the objects of a class that doesn't implement Comparable. ou can define a comparator to compare the elements of different classes.

What is comparing using natural order? What is comparing using comparator?

Comparing elements using the Comparable interface is referred to as comparing using natural order, and comparing elements using the Comparator interface is referred to as comparing using comparator.

When should you select an ArrayList over a LinkedList?

If you need to support random access through an index without inserting or removing elements at the beginning of the list.

When should you select a Linked List over an ArrayList?

If your application requires the insertion or deletion of elements at the beginning of the list, your should choose LinkedList.

What does the Collection interface do?

It defines the common operations for lists, vectors, stacks, queues, priority queues, and sets.

What does a LinkedList provide?

LinkedList provides methods for adding and inserting elements at both ends of the list. LinkedList is efficient at inserting and removing elements anywhere in the List.

Write a for-each loop to print our the string elements in a data structure such as a linkedlist.

LinkedList<String> list = new LinkedList<String>(); list.add("String1"); list.add("String2"); //for-each loop for(String element: list){ System.out.print(element + " "); } //This loop is read: for each element in list, do the following...

What is a List?

List is an interface that extends the Collection interface. A list adds position-oriented operations, as well as a new list iterator that enables the user to traverse the list bidirectionally. The list interface permits duplicates. A list can grow and shrink dynamically.

What does the public int compare(T element1, T element2) method return?

Returns a negative value if element1 is less than element2, a positive value if element1 is greater than element2, and 0 if they are equal.

What does public boolean equals(Object element) return?

Returns true if the specified object is also a comparator and imposes the same ordering as this comparator.

What are the 3 different kinds of collections?

Sets, Lists, and Queues.

What are the two types of containers the Java Collections Framework supports?

The Java Collections Framework supports collections and maps.

What is the ListIterator? Define its methods in UML format.

The ListIterator interface extends the Iterator interface to add bidirectional traversal of the list. +add(o: E): void +hasPrevious(): boolean +nextIndex(): int +previousIndex(): int +set(o: E): void

Why is the Java Collections Framework an excellent example of using interfaces, abstract classes, and concrete classes?

The interfaces define the framework. The abstract classes provide partial implementation. The concrete classes implement the interfaces with concrete data structures. Providing an abstract class that partially implements an interface makes it convenient for the user to write the code.

How do you add an element in a queue?

The offer method is used to add an element to the queue. This method is similar to the add method in the Collection interface, but the offer method is preferred for queues.

What's the difference between the poll() and remove() methods in the Queue<E> interface?

The poll and remove methods are similar, except that poll() returns null if the queue is empty, whereas remove() throws an exception.

How can you binary search through a list? How can you binary search through an array of strings?

Use Collections.binarySearch(list: List, key: Object) method to search through a list. Use Arrays.binarySearch(Object[], key: Object) method to search through an array of strings.

How do you obtain an iterator from a collection object?

Use the iterator method defined from the Iterable interface. For example: HashSet<String> hash = new HashSet<String>(); //create iterator object Iterator<String> iterator = hash.iterator();

Are all the methods in the Collections class static?

Yes.

What is a data structure?

also known as a container or container object, is an object that store other object, referred to as data or elements. To define a data structure is essentially to define a class. The class for a data structure should use data fields to store data and provide methods to support such operations as search, insertion, and deletion.

What method does Java provide to create a list with a variable number of arguments?

asList(arg[0], arg[1]... arg[n])... For example: List<String> list1 = Arrays.asList("red","green","blue");

How should you traverse each element in a Linked List? What operation should you avoid?

for(elementType s: linkedList){ process s; } //The for-each loop uses an iterator implicitly. Avoid using the get(i) method defined in for the Linked List. i.e. for(int i = 0; i < linkedList.size(); i++){ process linkedList.get(i); }

Which methods are in LinkedList but not in ArrayList?

getFirst(), getLast(), addFirst(), addLast() are methods in LinkedList but not in ArrayList.

Which package is the Comparable interface defined? Which package is the Comparator interface defined?

java.lang java.util

Which package contains all the interfaces define in the Java Collections Framework?

java.util

What two methods does the Comparator<T> interface contain?

public int compare(T element1, T element2) public boolean equals(Object element)

If a method has no meaning in a subclass, what method should be implemented?

public void someMethod(){ throw new UnsupportedOperationException ("This method is not supported"); }

What does a set do?

stores a group of NON-DUPLICATE elements.

What does a list do?

stores an ORDERED collection of elements.

What does a queue do?

stores objects that are processed in FIRST-IN,FIRST-OUT fashion.

What method do you use to obtain an element in the collection with an iterator?

use the iterator.next() method. For some iterator is: Iterator<E> iterator = new Collection<E>.iterator();


Set pelajaran terkait

Unit 1, Concept 3 --> Energy Flow

View Set

LAST BIOETHICS MOTHERUFCKING QUIZ

View Set

Possessive Pronouns & Contractions👍

View Set

Six Sigma Practices Questions 2--(ASQ Practice Question Set)

View Set

Weather Instruments and Vocabulary Terms 4th

View Set