package com.xayd_01;
public class Student {
private String name;
private int age;
public String getName () {
return name;
}
public void setName (String name) {
this .name = name;
}
public int getAge () {
return age;
}
public void setAge (int age) {
this .age = age;
}
public Student (){}
public Student (String name,int age){
this .name=name;
this .age=age;
}
}
package com.xayd_01;
import java.util.ArrayList;
import java.util.Collection;
/**
* 1.创建学生对象
* 2.创建collection集合并添加元素
* 3.将collection集合转化为数组
* 4.使用for循环遍历数组
* */
public class StudentDemo {
public static void main (String[] args) {
Student s1 = new Student("甲" ,20 );
Student s2 = new Student("乙" ,22 );
Student s3 = new Student("丙" ,23 );
Collection c = new ArrayList();
c.add(s1);
c.add(s2);
c.add(s3);
Object[] arr = c.toArray();
for (int i = 0 ; i < arr.length; i++) {
Student s = (Student) arr[i];
System.out.println(s.getAge()+" " +s.getName());
}
}
}
package com.xayd_02;
public class Dog {
private String name;
private int age;
public Dog () {
super ();
}
public Dog (String name, int age) {
super ();
this .name = name;
this .age = age;
}
public String getName () {
return name;
}
public void setName (String name) {
this .name = name;
}
public int getAge () {
return age;
}
public void setAge (int age) {
this .age = age;
}
}
package com.xayd_02;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
/**
* 1.创建一个狗的对象
* 2.创建一个集合
* 3.用iterator创建一个迭代器
* 4.使用next方法进行遍历
*/
public class DogDemo {
public static void main (String[] args) {
Dog d1 = new Dog("芊芊" ,2 );
Dog d2 = new Dog("乐乐" ,3 );
Dog d3 = new Dog("哈哈" ,1 );
Collection <Dog> c = new ArrayList<Dog>();
c.add(d1);
c.add(d2);
c.add(d3);
Iterator <Dog> it = c.iterator();
while (it.hasNext()){
Dog d = it.next();
System.out.println(d.getAge()+" " +d.getName());
}
}
}
package com.xayd_04;
public class Car {
private String brand;
private String color;
private int price;
public Car (String brand, String color, int price) {
super ();
this .brand = brand;
this .color = color;
this .price = price;
}
public Car () {
super ();
}
public String getBrand () {
return brand;
}
public void setBrand (String brand) {
this .brand = brand;
}
public String getColor () {
return color;
}
public void setColor (String color) {
this .color = color;
}
public int getPrice () {
return price;
}
public void setPrice (int price) {
this .price = price;
}
}
package com.xayd_04;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* 1.创建汽车类对象
* 2.创建list集合并添加对象
* 3.获取相应迭代器,并调用相关方法进行遍历
* 4.使用for,foreach进行遍历
* */
public class CarTest {
public static void main (String[] args) {
Car c1 = new Car("丰田" ,"红色" ,120000 );
Car c2 = new Car("本田" ,"蓝色" ,140000 );
Car c3 = new Car("尼桑" ,"银色" ,120000 );
List <Car> list = new <Car> ArrayList();
list.add(c1);
list.add(c2);
list.add(c3);
Iterator <Car> it = list.iterator();
while (it.hasNext()){
Car c = it.next();
System.out.println(c.getBrand()+" " +c.getPrice()+" " +c.getColor());
}
System.out.println("---------------------------------------" );
for (int i = 0 ; i < list.size(); i++) {
Car c = list.get(i);
System.out.println(c.getBrand()+" " +c.getColor()+" " +c.getPrice());
}
System.out.println("----------------------------------------------" );
for (Car c:list){
System.out.println(c.getBrand()+" " +c.getColor()+" " +c.getPrice());
}
}
}
package com.xayd00;
public class Student {
private String name;
private int age;
public Student () {
super ();
}
public Student (String name, int age) {
super ();
this .name = name;
this .age = age;
}
public String getName () {
return name;
}
public void setName (String name) {
this .name = name;
}
public int getAge () {
return age;
}
public void setAge (int age) {
this .age = age;
}
@Override
public int hashCode () {
final int prime = 31 ;
int result = 1 ;
result = prime * result + age;
result = prime * result + ((name == null ) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals (Object obj) {
if (this == obj)
return true ;
if (obj == null )
return false ;
if (getClass() != obj.getClass())
return false ;
Student other = (Student) obj;
if (age != other.age)
return false ;
if (name == null ) {
if (other.name != null )
return false ;
} else if (!name.equals(other.name))
return false ;
return true ;
}
}
package com.xayd00;
import java.util.HashSet;
public class HashSetDemo {
public static void main (String[] args) {
HashSet<Student> hs = new HashSet<Student>();
Student s1 = new Student ("周杰伦" ,35 );
Student s2 = new Student ("陈奕迅" ,40 );
Student s3 = new Student ("薛之谦" ,30 );
Student s4 = new Student ("杨宗纬" ,32 );
Student s5 = new Student ("林俊杰" ,36 );
hs.add(s1);
hs.add(s2);
hs.add(s3);
hs.add(s4);
hs.add(s5);
for (Student S: hs) {
System.out.println(S.getName()+" " +S.getAge());
}
}
}
package com.xayd01;
import java.util.TreeSet;
public class TreeSetDemo {
public static void main (String[] args) {
TreeSet <Integer> ts = new TreeSet <Integer>();
ts.add(20 );
ts.add(18 );
ts.add(23 );
ts.add(22 );
ts.add(17 );
ts.add(24 );
ts.add(19 );
ts.add(18 );
ts.add(24 );
for (Integer in : ts) {
System.out .print(in +" " );
}
}
}
package com.xayd02;
import java.util.HashMap;
import java.util.Set;
public class HashMapDemo {
public static void main (String[] args) {
HashMap<Student, String> hm = new HashMap<Student, String>();
Student s1 = new Student("杰克逊" , 60 );
Student s2 = new Student("孙楠" , 50 );
Student s3 = new Student("权志龙" , 30 );
Student s4 = new Student("权志龙" , 30 );
hm.put(s1, "美国" );
hm.put(s2, "中国" );
hm.put(s3, "韩国" );
hm.put(s4, "中国" );
Set<Student> keys = hm.keySet();
for (Student s : keys) {
System.out .println(s+" " +hm.get (s));
}
}
}