1.
import java.util.Random;
import java.util.Arrays;
public class TestlntArray {
public static void main(String[] args) {
int[] a = new int [10];
Random random = new Random();
for (int i = 0; i<a.length; i++) {
a[i] = random.nextInt(100)+1;
System.out.print(a[i] + " "); }
Arrays.sort(a);
System.out.println();
for (int e : a) {
System.out.print(e + " "); }
} }
import java.util.Arrays;
class Dog implements Comparable<Dog> {
String name;
int weight;
public Dog(String name, int weight) {
this.name = name;
this.weight = weight; }
@Override
public String toString() {
return this.name + "重" + this.weight + "公斤"; }
@Override
public int compareTo(Dog o){
if(!(o instanceof Dog)) return 1;
if(this.weight>o.weight) return 1;
if(this.weight<o.weight) return -1;
return 0;
}}
public class TestObjectArray {
public static void main(String[] args) {
Dog[] dogs = new Dog[3];
dogs[0] = new Dog("大黄", 30);
dogs[1] = new Dog("旺财", 20);
dogs[2] = new Dog("福娃", 10);
System.out.println("排序前:");
for ( Dog e:dogs) {
System.out.print(e + " "); }
Arrays.sort(dogs);
System.out.println("\n 排序后:");
for (int i = 0; i < dogs.length; i++) {
System.out.print(dogs[i] + " ");
}
} }
3.扩展练习 待更
4.
import java.util.Collections;
import java.lang.String;
import java.util.Iterator; import java.util.List;
import java.util.LinkedList;
import java.util.Collections;
import java.util.List;
class Dog implements Comparable<Dog> {
String name;
int weight;
public Dog(String name, int weight)
{
this.name = name;
this.weight = weight; }
@Override
public String toString() {
return this.name + "重" + this.weight + "公斤"; }
@Override
public int compareTo(Dog o) {
return this.weight - o.weight; }
}
public class TestList {
public static void main(String[] args) {
List<Dog> list = new LinkedList<>();
Dog dog1 = new Dog("dog1", 30);
Dog dog3 = new Dog("dog3", 10);
list.add(dog1);
list.add(dog3);
Dog dog2 = new Dog("dog2", 20);
int index =list.indexOf(dog3);
list.add(2,dog2);
System.out.println("使用增强型 for 循环遍历 list:");
for (Dog dog:list) {
System.out.print(dog + "; "); }
System.out.println("\n 使用 for 循环遍历 list:");
for (int i = 0; i<list.size(); i++) {
Dog dog = list.get(i);
System.out.print(dog + "; "); }
Collections.sort(list);
System.out.println("\n 排序后,使用迭代器遍历 list:");
Iterator<Dog> iterator = list.iterator();
while (iterator.hasNext()) {
Dog dog =iterator.next();
System.out.print(dog+" ");
}
list.remove(dog2);
System.out.println("\n 删除一个元素后,list 中的元素数量是:" + list.size());
list.clear();
System.out.println("list 是否为空:" + (null == list || list.size() ==0 ) );
} }
import java.util.*;
import java.util.Collections;
import java.util.Collections;
import java.lang.String;
import java.util.SortedMap;
class MyKey implements Comparable<MyKey> {
int key;
public MyKey(int key){
this.key=key;
}
@Override
public String toString(){
return "key="+ key;
}
public int compareTo(MyKey o){
if(this.key>o.key) return 1;
if(this.key<o.key) return -1;
return 0;
}
public int hashCode(){
return this.key*77;
}
public boolean equals(Object obj){
MyKey a=(MyKey)obj;
if(this.key==a.key){
return true;
}
else
return false;
}
}
class MyValue{
String value;
public MyValue(String value){
this.value=value;
}
@Override
public String toString(){
return "value=" +value;
}
}
public class TestMap {
public static void main(String[] args) {
MyKey key1 = new MyKey(1);
MyKey key2 = new MyKey(2);
MyKey key3 = new MyKey(3);
MyValue value1 = new MyValue("a");
MyValue value2 = new MyValue("b");
MyValue value3 = new MyValue("c");
Map<MyKey, MyValue> hashMap = new HashMap<MyKey, MyValue>();
hashMap.put(key1,value1);
hashMap.put(key2,value2);
hashMap.put(key3,value3);
System.out.println("hashMap 中元素的数量:" + hashMap.size());
MyKey key4 = new MyKey(3);
MyValue value4 = new MyValue("d");
hashMap.put(key4,value4);
System.out.println("遍历 hashMap 中的 entry");
Set<Map.Entry<MyKey, MyValue>> entries = hashMap.entrySet();
for(Map.Entry<MyKey, MyValue> entry : entries) {
MyKey myKey = entry.getKey();
MyValue myValue = entry.getValue();
System.out.println("<" + myKey + " , " + myValue + ">");
}
System.out.println("是否包含key3:" + hashMap.containsKey(key3));
System.out.println("是否包含 value3:" + hashMap.containsValue(value3));
SortedMap<MyKey, MyValue> treeMap = new TreeMap(hashMap);
System.out.println("遍历 treeMap 中的 entry");
Set<Map.Entry<MyKey, MyValue>> t = treeMap.entrySet();
for(Map.Entry<MyKey, MyValue> entry : t) {
MyKey myKey = entry.getKey();
MyValue myValue = entry.getValue();
System.out.println("<" + myKey + " , " + myValue + ">");
}
}
}