Video https://edu.csdn.net/course/play/7811
Exam aquestion 1
Given a Pre-generics implementation of a method:
One. public static int sum (List list) {
sum of int = 0;
for (Iterator iter = List.iterator (); Iter.hasnext ();) {
int i = ((Integer) Iter.next ()). Intvalue ();
sum = i;
16.}
return sum;
18.}
What three changes allow the class to is used with generics avoid and unchecked a warning? (Choose
Three.)
Change which three places allow this class to use generics and avoid an unchecked warning
A. Remove line 14.
B. Replace line with "int i = Iter.next ();".
C. Replace line with ' for (int i:intlist) {".
D. Replace line with ' for (iterator iter:intlist) {".
E. Replace the method declaration with "sum (list<int> intlist)".
F. Replace the method declaration with "sum (list<integer> intlist)".
Answer:acf
Analysis: Note that the question here is to change the three, without affecting the program's warning, while changing, remember to remember ...
Question 2
A programmer has an algorithm this requires a java.util.List that provides a efficient implementation of
Add (0, object), but does not need to support quick random access. What supports these requirements?
Translation: Programmers have an algorithm that requires a java.util.List to provide an effective implementation of Add (0, object), but does not need to support fast random access. What is the support of these requirements? A. Java.util.Queue
B. java.util.ArrayList
C. java.util.LinearList
D. java.util.LinkedList
Answer:d
Queue (queue), no Add () method.
ArrayList class, with Add (0,object) method, support random access (random access)
Linearlist don't have this stuff.
LinkedList: Linked list needs to be traversed from begin () to end (), the complexity is O (n); random access is ArrayList.
Linkedlist<e>
Extends abstractsequentiallist<e>
Implements List<e>, Queue<e>, Cloneable, java.io.Serializable
This class implements the queue interface, providing advanced first out queue operations for Add, poll, and so on.
Question 3
Given:
One.//Insert code here
Private N min, Max;
Public N Getmin () {return min;}
Public N Getmax () {return max;}
public void Add (N added) {
if (min = null | | added.doublevalue () < Min.doublevalue ())
Min = added;
if (max = null | | added.doublevalue () > Max.doublevalue ())
max = added;
20.}
21.}
Which two, inserted at line, would allow the code to compile? (Choose two.)
What are the two items that you can use to compile code when inserting code in 11 lines? A. public class Minmax<?> {
B. public class minmax<? Extends Number> {
C. public class Minmax<n extends Object> {
D. public class Minmax<n extends Number> {
E. public class minmax<? Extends Object> {
F. public class Minmax<n extends Integer> {
Answer:df
12 lines of code, defined variable Min,max, method Doublevalue (), you can know that min and Max are abstract class number or instantiated objects of their subclasses.
Number abstract class, which has the following methods:
Question 4
Given:
Import java.util.*;
public class Explorer2 {
public static void Main (string[] args) {
treeset<integer> s = new treeset<integer> ();
treeset<integer> subs = new treeset<integer> ();
for (int i = 606 i < 613; i++)
if (i%2 = 0) S.add (i);
Subs = (TreeSet) s.subset (608, True, 611, true);
S.add (629);
SYSTEM.OUT.PRINTLN (S + "" + subs);
22.}
23.}
What is the result?
A. Compilation fails.
B. An exception are thrown at runtime.
C. [608, 610, 612, 629] [608, 610]
D. [608, 610, 612, 629] [608, 610, 629]
E. [606, 608, 610, 612, 629] [608, 610]
F. [606, 608, 610, 612, 629] [608, 610, 629]
Answer:e
Main examination:
1, TreeSet Natural order
2, the use of subset () method
If the classmate, you even TreeSet is a nothing understand, need to go down the treeset aspects of the technical points.
If you have a classmate, you may ask what it means.
In the loop, 18 lines of code, the value of S is [606,608,610,612], and then 629 is added to the 20 line of code, so the last S store value is [606,608,610,612,629]
The rule of law is E.
Returns a partial view of this set whose elements are from fromelement (including) to Toelement (not included). (if fromelement is equal to Toelement, the returned ordered set is empty.) The ordered set returned is supported by this set, so changes in the returned sorted set are reflected in this set, and vice versa. The sorted set returned supports all optional set operations.
If the user attempts to insert an element other than the specified range, the sorted set returned by this method throws a illegalargumentexception.
19 lines of code, assigning to subs this TreeSet object, the subset () method, similar to the intercept string of string, where a partial tree,true represents the containing element node, and False indicates that the element node is not included. Because it is the containing node, subs is [608,610].
Question 5
Given:
1. Public class Score implements comparable<score> {
2. Private int wins, losses;
3. Public Score (int w, int l) {wins = W; losses = l;}
4. public int getwins () {return wins;}
5. public int getlosses () {return losses;}
6. Public String toString () {
7. Return "<" + wins + "," + Losses + ">";
8.}
9.//Insert code here
10.}
Which method would complete this class?
Which of the following methods can be used to supplement this class on line 9th?
A. public int compareTo (Object o) {/*more code here*/}
B. public int CompareTo (Score other) {/*more code here*/}
C. public int Compare (Score s1,score s2) {/*more Code here*/}
D. public int Compare (Object o1,object O2) {/*more Code here*/}
Answer:b
/**
* Comparable interface implementation;
* @author Zhang Chenguang
* * */public
class Score implements comparable<score>{
//wins: Victory; Lossess: Loss (loss)
private int wins,losses;
Public Score (int w,int l) {wins=w;losses=l;}
public int Getwins () {return wins;}
public int getlosses () {return losses;}
Public String toString () {return
"<" +wins+ "," +losses+ ">;
}
@Override public
int compareTo (Score o) {return
0;
}
}
Comparable<t>
This interface forces an overall ordering of the objects for each class that implements it. This sort is called the natural ordering of the class, and the CompareTo method of the class is called its natural comparison method. Comparabale interface needs to implement CompareTo method generics are score
CompareTo is to compare two values, if the former is greater than the latter, return 1, equal to return 0, less than return-1
Whether this interface or compartor, you need to be consistent with the generic parameters, as shown in the following figure:
Another comparison interface: