Questions:
Questions:
import java.util.ArrayList;
import java.util.List;
public class Demo {
public static void main(String[] args) {
List<Integer> a = new ArrayList<>();
a.add(1);
a.add(2);
for (int i : a){
System.out.println(i);
}
}
}public class Demo{
public static void main(String[] args) {
int[][] a = new int[2][2];
int[][] b = {{1,2,3},{4,5,6},{7,8,9}};
}
}import java.util.Set;
import java.util.HashSet;
public class Demo{
public static void main(String[] args) {
Set<Integer> a = new HashSet<>();
a.add(1);
a.add(2);
a.add(1);
}
}Questions:
- Can a Set contain duplicate elements?
import java.util.Queue;
import java.util.LinkedList;
public class Demo{
public static void main(String[] args) {
Queue<Integer> a = new LinkedList();
a.offer(1);
a.offer(2);
a.offer(3);
a.poll();
}
}import java.util.Stack;
public class Demo{
public static void main(String[] args) {
Stack<Integer> a = new Stack<>();
a.push(1);
a.push(2);
a.push(3);
a.pop();
}
}Questions:
- Is Queue First-In-First-Out or Last-In-First-Out? How about Stack?
- Describe some examples of the implementations on queue and stack?
import java.util.Map;
import java.util.HashMap;
public class Demo{
public static void main(String[] args) {
Map<Integer, Integer> a = new HashMap<>();
a.put(1, 1);
a.put(1, 2);
}
}Questions:
- What's stored in a Map?
- Can Map have duplicate key or value?
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
public class Demo{
public static void main(String[] args) {
Person a = new Person(19);
Person b = new Person(20);
Person c = new Person(18);
List<Person> list = new ArrayList<Person>();
list.add(a);
list.add(b);
list.add(c);
System.out.println(list.get(0).age);
Collections.sort(list);
System.out.println(list.get(0).age);
}
}
class Person implements Comparable<Person>{
int age;
Person(int i){
this.age = i;
}
public int compareTo(Person a){
return this.age - a.age;
}
}Questions:
- How do you understand sort?
- What does public int compareTo() stand for?
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class Demo{
public static void main(String[] args) {
Person a = new Person(19);
Person b = new Person(20);
Person c = new Person(18);
List<Person> list = new ArrayList<Person>();
list.add(a);
list.add(b);
list.add(c);
System.out.println(list.get(0).age);
AgeCompare ageCompare = new AgeCompare();
Collections.sort(list, ageCompare);
System.out.println(list.get(0).age);
}
}
class Person{
int age;
Person(int i){
this.age = i;
}
}
class AgeCompare implements Comparator<Person>{
public int compare(Person a, Person b){
return a.age - b.age;
}
}Questions:
- What's the difference between Comparable and Comparator?
- For method public int compare(), what does positive, zero and negative return value stand for?