package testList;
import java.util.ArrayList;
import java.util.Vector;
/**
*@auther WangZhi
*@date 2019/8/22 13:56
*/
public class TestList {
/*
List:
所属包:java.util
add,get,remove,改set,个数size
由于ArrayList底层是一个Object[] 什么类型都可以存进去,取出来的时候多态的
效果,需要自己造型 显得用起来非常的麻烦
JDK1.5之后 ---> 泛型
泛型是用来规定数据类型的,定义的时候用一个符号代替某中类型,在使用的时候用具体的
数据类型 将定义的那个符号替换掉
泛型可以用在哪里
泛型不能使用基本类型 可以用对应的包装类
1.泛型类
类定义的时候描述某种数据类型 集合的使用就是这样
2.泛型接口
与泛型类的使用基本一致 子类实现接口时必须添加泛型
**/
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("a");
list.add("c");
list.add("d");
list.add("e");
list.add("f");
list.add("g");
System.out.println(list.size());
for (int i = 0; i < list.size(); i++) {
String value = list.get(i);
System.out.println(value);
}
ArrayList<Integer> list1 = new ArrayList<>();
list1.add(10);
list1.add(20);
list1.add(30);
list1.add(40);
list1.add(50);
list1.add(60);
ArrayList<Integer> list2 = new ArrayList<>();
list2.add(100);
list2.add(200);
list2.add(300);
list1.addAll(list2);
System.out.println(list1);
//清除集合元素
//list1.clear();
//System.out.println(list1);
boolean is = list1.contains(10);
System.out.println(is);
/*
retainAll();
subList();
indexOf();
**/
/*
Vector类
所属包:java.util
Vector 底层也是利用数组(动态)的形式储存
Vector 是线程同步的(synchronize)安全性高 效率低
扩容方式:默认扩容2倍 可以通过构造方法创建对象时修改这一机制
**/
Vector<String> vector = new Vector<>();
vector.add("a");
vector.add("b");
vector.add("c");
System.out.println(vector);
//修改
System.out.println(vector.set(1,"qqqqqq"));
System.out.println(vector);
/*总结
— List 有序,可重复
ArrayList
优点: 底层数据结构是数组,查询快,增删慢。
缺点: 线程不安全,效率高
Vector
优点: 底层数据结构是数组,查询快,增删慢。
缺点: 线程安全,效率低
LinkedList
优点: 底层数据结构是链表,查询慢,增删快。
缺点: 线程不安全,效率高
—Set 无序,唯一
HashSet
底层数据结构是哈希表。(无序,唯一)
如何来保证元素唯一性?
1.依赖两个方法:hashCode()和equals()
LinkedHashSet
底层数据结构是链表和哈希表。(FIFO插入有序,唯一)
1.由链表保证元素有序
2.由哈希表保证元素唯一
TreeSet
底层数据结构是红黑树。(唯一,有序)
1. 如何保证元素排序的呢?
自然排序
比较器排序
2.如何保证元素唯一性的呢?
根据比较的返回值是否是0来决定
针对Collection集合我们到底使用谁呢?(掌握)
唯一吗?
是:Set
排序吗?
是:TreeSet或LinkedHashSet
否:HashSet
如果你知道是Set,但是不知道是哪个Set,就用HashSet。
否:List
要安全吗?
是:Vector
否:ArrayList或者LinkedList
查询多:ArrayList
增删多:LinkedList
如果你知道是List,但是不知道是哪个List,就用ArrayList。
如果你知道是Collection集合,但是不知道使用谁,就用ArrayList。
如果你知道用集合,就用ArrayList。
**/
//Map集合的几个接口
HashSet<String> hashSet = new HashSet<>();
LinkedHashSet<String> linkedHashSet = new LinkedHashSet<>();
TreeSet<String> treeSet = new TreeSet<>();
/*//TreeSet第一种基本数据类型排序方法
treeSet.add("g");
treeSet.add("e");
treeSet.add("f");
treeSet.add("d");
treeSet.add("b");
treeSet.add("c");
treeSet.add("a");
for (String v : treeSet) {
System.out.print(v);
//abcdefg
}*/
//TreeSet第二种引用数据类型排序
TreeSet<Student> studet = new TreeSet<>();
Student students1 = new Student("zhangsan",20);
Student students2 = new Student("lis",22);
Student students3 = new Student("wangwu",24);
Student students4 = new Student("chenliu",26);
Student students5 = new Student("zhangsan",22);
Student students6 = new Student("qianqi",24);
studet.add(students1);
studet.add(students2);
studet.add(students3);
studet.add(students4);
studet.add(students5);
studet.add(students6);
for (Student s : studet) {
System.out.println(s.getName()+"---"+s.getAge());
}
/*hashSet.add("b");
hashSet.add("a");
hashSet.add("d");
hashSet.add("c");
hashSet.add("e");
for (String h : hashSet) {
System.out.print(h);
//abcde 有序排列
}
System.out.println();
linkedHashSet.add("b");
linkedHashSet.add("a");
linkedHashSet.add("c");
linkedHashSet.add("e");
linkedHashSet.add("d");
for (String l : linkedHashSet) {
System.out.print(l);
//baced 无序排列
}
System.out.println();
treeSet.add("b");
treeSet.add("a");
treeSet.add("c");
treeSet.add("e");
treeSet.add("d");
for (String t : treeSet) {
System.out.print(t);
//abcde 有序排列
}
System.out.println();
HashMap<String, String> hashMap = new HashMap<>();
hashMap.put("a", "r");
hashMap.put("d", "f");
hashMap.put("b", "g");
hashMap.put("c", "h");
for (String v : hashMap.keySet() ){
System.out.print(hashMap.get(v));
//rghf 无序
}
System.out.println();
Hashtable<Integer,Integer> hashtable = new Hashtable<>();
hashtable.put(1,3);
hashtable.put(4,1);
hashtable.put(3,2);
hashtable.put(2,6);
for (Integer v1 : hashtable.keySet()) {
System.out.print(hashtable.get(v1));
//1263 无序
}
System.out.println();
TreeMap<Integer,Integer> treeMap = new TreeMap<>();
treeMap.put(2,3);
treeMap.put(1,4);
treeMap.put(4,2);
treeMap.put(3,1);
for (Integer v2 : treeMap.keySet()) {
System.out.print(treeMap.get(v2));
//4312 有序
}*/
}
}
//Set元素是无序不重复
/*HashSet<String> set = new HashSet<>();
set.add("B");
set.add("b");
set.add("a");
set.add("A");
set.add("C");
set.add("c");
Iterator<String> iterator = set.iterator();
while (iterator.hasNext()) {
String value = iterator.next();
System.out.println(value );
}*/
/*
HashSet 无重复的原则
1.首先通过String类型 和 Person类型进行储存
2.无重复的原则可能是利用equals进行补缴 (下面案例一开始是Person长度是5, String长度为1)
3.如果想让Person对象和String一至 也认为是1个对象不重复
4.可以尝试充血equals方法
5.结果单单equals的重写并不能达到预期的效果 那么可能还有一个需要重写HashCode
6.set集合发现重复元素拒绝存入
*/
HashSet<Person> person = new HashSet<Person>();
person.add(new Person("道衍",5));
person.add(new Person("道衍",2));
person.add(new Person("道衍",3));
person.add(new Person("道衍",4));
person.add(new Person("道衍",1));
System.out.println(person.size());
//5 -->如果重写了equals和hashCode两个方法 那么size() 输出1
//System.out.println(person.iterator().next().getNum());
System.out.println(person);
HashSet<String> str = new HashSet<>();
str.add(new String("道衍"));
str.add(new String("道衍"));
str.add(new String("道衍"));
str.add(new String("道衍"));
str.add(new String("道衍"));
System.out.println(str.size());
package testMap;
/**
* Author : 道衍
* date: 2019-08-26-上午11:50
* Description: 描述:
*/
public class Person {
private String name;
private int num;
public Person(String name, int num) {
this.name = name;
this.num = num;
}
public String getName() {
return this.name;
}
public int getNum() {
return this.num;
}
//重写equals方法 将Person 放入到set集合中 去掉重复值
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof Person) {
//obj 还原Person原型
Person anotherPerson = (Person)obj;
//this anotherPerson 进行比较对象中的name属性
if (this.name.equals(anotherPerson.name)) {//递归
return true;
}
}
return false;
}
//重写HashCode
public int hashCode() {
//两个Person对象 name属相一至 需要让hashcode值返回一至
return this.name.hashCode();
}
//重写toString 方法 让对象打印输出的时候直接输出对象的属性, 而不是hashCode码
public String toString() {
StringBuilder builder = new StringBuilder("{");
builder.append(this.name);
builder.append(",");
builder.append(this.num);
builder.append("}");
return new String(builder);
//return "{"+this.name+","+this.num+"}";
}
}
//TreeSet
/*
//如果想要让Person对象存入到treeSet集合内,必须实现接口Comparable
*/
package testMap;
import java.util.TreeSet;
/**
* Author : 道衍
* date: 2019-08-26-下午1:35
* Description: 描述:
*/
public class TreeSetTest {
public static void main(String[] args) {
TreeSet<Person> person = new TreeSet<Person>();
person.add(new Person("道衍",5));
person.add(new Person("道衍",2));
person.add(new Person("道衍",3));
person.add(new Person("道衍",4));
person.add(new Person("道衍",1));
System.out.println(person);
System.out.println(person.size());
}
}
package testMap;
/**
* Author : 道衍
* date: 2019-08-26-上午11:50
* Description: 描述:
*/
public class Person implements Comparable<Person> {
private String name;
private int num;
public Person(String name, int num) {
this.name = name;
this.num = num;
}
public String getName() {
return this.name;
}
public int getNum() {
return this.num;
}
//重写equals方法 将Person 放入到set集合中 去掉重复值
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof Person) {
//obj 还原Person原型
Person anotherPerson = (Person)obj;
//this anotherPerson 进行比较对象中的name属性
if (this.name.equals(anotherPerson.name)) {//递归
return true;
}
}
return false;
}
//重写HashCode
public int hashCode() {
//两个Person对象 name属相一至 需要让hashcode值返回一至
return this.name.hashCode();
}
//重写toString 方法 让对象打印输出的时候直接输出对象的属性, 而不是hashCode码
public String toString() {
StringBuilder builder = new StringBuilder("{");
builder.append(this.name);
builder.append(",");
builder.append(this.num);
builder.append("}");
return new String(builder);
//return "{"+this.name+","+this.num+"}";
}
//如果想要让Person对象存入到treeSet集合内,必须实现接口Comparable 重写这个方法
@Override
public int compareTo(Person o) {
return (this.name.compareTo(o.name));
//当前对象的name和另一个对象name的compareTo结果
}
}
package testMap;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
/**
* Author : 道衍
* date: 2019-08-26-下午2:58
* Description: 描述:
*/
public class MapTest {
/**
* Map 映射, 通过key可以直接定位到一个value值
* 储存的方式是键值对 key -- value
* key无序无重复 value无序可重复
* key无序还是一直, 指的是存入顺序和取值顺序不一致
* key无重复当然指的就是元素不能一致
*/
/**
* map的基本使用
* 1.HashMap
* 如何创建对象:
* 方法:增删改查
* put:增
* 1.key储存的顺序和取得顺序不同
* 2.不同的key可以储存相同的value
* 3.key如果有相同的,会将原有的value覆盖,不会不存 (和set相反)
* remove:删
* put:改
* replace:改
* get:查
* 还有些其他的方法:
* clear();
* contiansKey(key);
* Map集合在什么情况下使用:
* 1.想要存一组元素
* 数组 或 集合
* 如果储存元素的长度以后不变 用数组
* 如果长度以后不确定用集合
* 2.如果长度以后不确定 --->集合
* 储存有序的用 List
* ArrayList 适合遍历查询
* LinkedList 是个插入和删除
* 储存元素希望自动去掉重复元素用 Set
* Hash 性能更高
* Tree 自动去重复, 还能自动排序
* map key--value 通过唯一的Key快速的找到value
* Hash 性能更高
* Tree 希望存进去的key自动排序
*
* 2.TreeMap
* 3.Properties(读取文件的信息)
*/
public static void main(String[] args) {
HashMap<Integer,String> map = new HashMap<Integer,String>();
map.put(2,"aaa");
map.put(1,"qqq");
map.put(4,"ddd");
map.put(3,"ddd");
map.put(5,"eee");
map.put(2,"xxx");
map.put(6,"ggg");
System.out.println(map);
//{1=qqq, 2=xxx, 3=ddd, 4=ddd, 5=eee, 6=ggg}
/**
* map集合的遍历
* 先获取所有的key
*
*/
Set<Integer> keys = map.keySet();
//通过迭代器遍历所有的key
Iterator<Integer> it = keys.iterator();
while (it.hasNext()) {
Integer key = it.next();
String value = map.get(key);
System.out.println(key+"---"+value );
}
}
}
package testMap;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
/**
* Author : 道衍
* date: 2019-08-26-下午2:58
* Description: 描述:
*/
public class MapTest {
/**
* Map 映射, 通过key可以直接定位到一个value值
* 储存的方式是键值对 key -- value
* key无序无重复 value无序可重复
* key无序还是一直, 指的是存入顺序和取值顺序不一致
* key无重复当然指的就是元素不能一致
*/
/**
* map的基本使用
* 1.HashMap
* 如何创建对象:
* 方法:增删改查
* put:增
* 1.key储存的顺序和取得顺序不同
* 2.不同的key可以储存相同的value
* 3.key如果有相同的,会将原有的value覆盖,不会不存 (和set相反)
* remove:删
* put:改
* replace:改
* get:查
* 还有些其他的方法:
* clear();
* contiansKey(key);
* Map集合在什么情况下使用:
* 1.想要存一组元素
* 数组 或 集合
* 如果储存元素的长度以后不变 用数组
* 如果长度以后不确定用集合
* 2.如果长度以后不确定 --->集合
* 储存有序的用 List
* ArrayList 适合遍历查询
* LinkedList 是个插入和删除
* 储存元素希望自动去掉重复元素用 Set
* Hash 性能更高
* Tree 自动去重复, 还能自动排序
* map key--value 通过唯一的Key快速的找到value
* Hash 性能更高
* Tree 希望存进去的key自动排序
*
* HashMap 底层数据结构储存:
* 散列表形式 数组 + 链表
* hashCode方法 --> 不同的对象 可以产生相同的hashCode码
* 不同的hashCode码 --> 对象肯定不同
*
* 2.TreeMap
* 常用方法:
* put, get, remove, replace, size
* 3.Properties(读取文件的信息)
*/
public static void main(String[] args) {
HashMap<Integer,String> map = new HashMap<Integer,String>();
map.put(2,"aaa");
map.put(1,"qqq");
map.put(4,"ddd");
map.put(3,"ddd");
map.put(5,"eee");
map.put(2,"xxx");
map.put(6,"ggg");
System.out.println(map);
//{1=qqq, 2=xxx, 3=ddd, 4=ddd, 5=eee, 6=ggg}
/**
* map集合的遍历
* 先获取所有的key
*
*/
Set<Integer> keys = map.keySet();
//通过迭代器遍历所有的key
Iterator<Integer> it = keys.iterator();
while (it.hasNext()) {
Integer key = it.next();
String value = map.get(key);
System.out.println(key+"---"+value );
}
}
}
package testMap;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
/**
* Author : 道衍
* date: 2019-08-26-下午4:07
* Description: 描述:
*/
public class LoginService {
//设计一个方法 用来登入认证 一维数组
/*private String[] userBox = new String[]{"道衍","道野","汉武大帝"};
private int[] passwordBox = new int[]{111,222,333};
public String login(String name,String password) {
for (int i = 0;i < userBox.length;i++) {
if (userBox[i].equals(name)) {
if (passwordBox[i] == Integer.parseInt(password)) {
return "登入成功!";
}
break;
}
}
return "登入失败!账号或密码错误!";
}*/
//第二种方式集合的方式
/*private ArrayList<String> userBox = new ArrayList<>();
{
userBox.add("道衍-123");
userBox.add("道野-456");
}
public String loginForList (String name, String password) {
for (int i = 0;i < userBox.size();i++) {
String[] value = userBox.get(i).split("-");
if (value[0].equals(name)) {
if (value[1].equals(password)) {
return "等入成功!";
}
break;
}
}
return "登入失败!";
}*/
//第三种方法 Set
/*private HashSet<String> set = new HashSet<>();
{
set.add("道衍-123");
set.add("道野-123");
}
public String loginForSet(String name, String password) {
for (int i = 0;i < set.size();i++) {
Iterator<String> iterator = set.iterator();
while (iterator.hasNext()) {
String[] value = iterator.next().split("-");
if (value[0].equals(name)) {
if (value[1].equals(password)) {
return "登入成功!";
}
}
}
}
return "登入失败!";
}*/
//第四种方法
private HashMap<String,Integer> map = new HashMap<>();
{
map.put("道衍",123);
map.put("道野",333);
}
public String loginForMap (String name, String password) {
Integer passworldValue = map.get(name);
if (passworldValue != null && passworldValue.equals(Integer.parseInt(password))) {
return "登入成功!";
}
return "登入失败!";
}
}
/集合的小测试
package exam;
import java.util.ArrayList;
import java.util.Scanner;
/**
* Author : 道衍
* date: 2019-08-27-上午9:24
* Description: 描述:
*/
public class Student {
private String username;
private String password;
public Student(String username, String password) {
this.username = username;
this.password = password;
}
public String getUsername() {
return this.username;
}
public String getPassword() {
return this.password;
}
//学生考试 方法
//参数 --> 一套试卷 返回值 学生作答的所有选项 String 类型的数组
public String[] exam (ArrayList<Question> paper) {
String[] answers = new String[paper.size()];
Scanner input = new Scanner(System.in);
for (int i = 0;i < paper.size();i++) {
Question question = paper.get(i);//题干 答案
System.out.println((i + 1)+"."+question.getTitle());
System.out.println("请输入答案:");
answers[i] = input.nextLine();//接收学生输入的选项
}
return answers;
}
}
--------------------
package exam;
import java.util.ArrayList;
/**
* Author : 道衍
* date: 2019-08-27-上午9:25
* Description: 描述:
*/
public class Teacher {
//老师负责批卷子
//参数 学生答题的答案 真是的试卷,和学生的一致
//返回值int
public int checkPaper(ArrayList<Question> paper, String[] answers) {
int score = 0;//分数
for (int i = 0;i < paper.size();i++) {
Question question = paper.get(i);
if (question.getAnswer().equalsIgnoreCase(answers[i])) { //equalsIgnoreCase 大小写问题
score += (100/paper.size());
}
}
return score;
}
}
---------------------
package exam;
/**
* Author : 道衍
* date: 2019-08-27-上午9:45
* Description: 描述:
*/
public class Question {
private String title;//题目题干
private String answer;//题目答案
public Question(String title, String answer) {
this.title = title;
this.answer = answer;
}
public String getTitle() {
return this.title;
}
public String getAnswer() {
return this.answer;
}
//重写方法 将默认比较题目对象地址的规则 改成比较题干 题干一致则默认是同一条题目
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof Question) {
Question anotherQuestion = (Question)obj;
//this.title 按照 ? 截取 与 anotherQuestion.title?截取之前的部分比较
if (this.title.equals(anotherQuestion.title)) {
return true;
}
}
return false;
}
public int hashCode() {
return this.title.hashCode();
}
}
---------------------
package exam;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Random;
/**
* Author : 道衍
* date: 2019-08-27-上午9:24
* Description: 描述:
*/
public class ExamMachine {
/**
* 属性 -- 题库 好多个question类型的对向, 每一个对象就是一道题目
* Set集合 如果题库扩充就可去掉重复的题目
* hash集合遵循的规则是 equals 和 hashCode
*/
//记录学生账号和密码
private HashMap<String,String> userBox = new HashMap<>();
{
userBox.put("daoyan","123");
userBox.put("java","222");
userBox.put("web","333");
}
private HashSet<Question> questionBank = new HashSet<>();
//利用块来初始化HashSet集合内的对象
{
questionBank.add(new Question("以下哪个是JAVA基本数据类型?\n\tA.String\n\tB.Integer\n\tC.Boolean\n\tD.Math","C"));
questionBank.add(new Question("以下哪个不是JAVA基本数据类型?\n\tA.String\n\tB.int\n\tC.Boolean\n\tD.Double","A"));
questionBank.add(new Question("以下哪个是JAVA引用数据类型?\n\tA.String\n\tB.int\n\tC.Boolean\n\tD.char","A"));
questionBank.add(new Question("以下哪个不 是JAVA基本数据类型?\n\tA.String\n\tB.Integer\n\tC.Boolean\n\tD.Math","C"));
questionBank.add(new Question("以下哪个是JAVA.UTIL包中的类?\n\tA.String\n\tB.Integer\n\tC.Scanner\n\tD.Math","C"));
questionBank.add(new Question("以下哪个不是JAVA.UTIL包中的类?\n\tA.Date\n\tB.Integer\n\tC.Calender\n\tD.Random","B"));
questionBank.add(new Question("以下哪个不是String类中的方法?\n\tA.compareTo \n\tB.append\n\tC.sunString\n\tD.concat ","B"));
questionBank.add(new Question("以下哪个是是String类中的方法?\n\tA.append\n\tB.delete\n\tC.insert\n\tD.concat","D"));
questionBank.add(new Question("以下哪个不是接口中属性的修饰符?\n\tA.public\n\tB.static\n\tC.final\n\tD.abstract","D"));
questionBank.add(new Question("以下哪个不是set集合的方法?\n\tA.set\n\tB.add \n\tC.remove\n\tD.iterator","A"));
}
//设计一个方法 随机生成试卷
//参数 确定试卷5道题 返回值 ---> 试卷ArrayList<Question>
public ArrayList<Question> getPaper() {
System.out.println("正在随机生成考试卷,请耐心等待!");
try{
Thread.sleep(5000);
}catch (Exception e) {
e.printStackTrace();
}
//随机抽取试卷的时候, 试卷是不重复的
// set 存 ---> ArrayList
HashSet<Question> paper = new HashSet<>();
//产生一个随机号, 去寻找题目 题库是一个set 没有序号 把题库变成有序的 --->ArrayList
ArrayList<Question> questionBank = new ArrayList<>(this.questionBank);
//随机抽题
while (paper.size() != 5) {
int index = new Random().nextInt(this.questionBank.size());
paper.add(questionBank.get(index));
}
return new ArrayList<Question>(paper);
}
public String login(String username, String password) {
String realPassword = this.userBox.get(username);
if (realPassword != null && realPassword.equals(password)) {
return "登入成功!";
}
return "用户名或密码错误!";
}
}
-----------------------
package exam;
import java.util.ArrayList;
import java.util.Scanner;
/**
* Author : 道衍
* date: 2019-08-27-上午9:36
* Description: 描述:
*/
public class TestMain {
public static void main(String[] args) {
//创建考试机
ExamMachine machine = new ExamMachine();
Scanner input = new Scanner(System.in);
System.out.println("请输入用户名:");
String username = input.nextLine();
System.out.println("请输入密码:");
String password = input.nextLine();
//创建学生对象
Student student = new Student(username,password);
String result = machine.login(student.getUsername(),student.getPassword());
if (result.equals("登入成功!")) {
System.out.println("已登入!\n"+student.getUsername()+"开始考试!");
ArrayList<Question> paper = machine.getPaper();//随机抽取的试卷
String[] answers = student.exam(paper);
Teacher teacher = new Teacher();
int score = teacher.checkPaper(paper,answers);
System.out.println(student.getUsername()+"的成绩为"+score);
}
}
}