上面是今天所要学习的内容模块。
@1回顾集合Arraylist.
运行结果
package Collection1; import java.util.ArrayList; public class ArrayListDemo1 { public static void main(String[] args) { ArrayListlist =new ArrayList(); list.add(56); list.add(10); list.add(58); list.add(890); list.add(786); list.add(89); list.add(565); list.add(678); //list.add(" l love you "); list.add(90); list.add(78); list.remove(2); list.remove(new Integer(58)); //删除指定内容元素 //list.remove(" l love you"); list.remove(1); System.out.println(list.size()); //修改 list.set(2, 1); //查询数据 Object obj =list.get(0); System.out.println(list.get(0)); list.get(2); System.out.println(list.get(2)); System.out.println(list); for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); } } }
@2程序异常的一些常看到的错误。
来了解一下吧.
第一种错误: java.lang.ArithmeticException: / by zero
at ExceptionDemo2.ExceptionDemo1.main(ExceptionDemo1.java:8)
第二种错误。 java.lang.NumberFormatException: For input string: "abd"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.(Integer.java:867)
at ExceptionDemo2.ExceptionDemo2.main(ExceptionDemo2.java:7)
第三种错误。java.lang.NumberFormatException: For input string: "abd"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.(Integer.java:867)
at ExceptionDemo2.ExceptionDemo3.main(ExceptionDemo3.java:34)
第四种错误。 java.lang.ArrayIndexOutOfBoundsException: 3@3Collection的集合内容.
at Throw.ThorowDemo1.method1(ThorowDemo1.java:35)
at Throw.ThorowDemo1.main(ThorowDemo1.java:14) 代码内容。package Throw; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class ThorowDemo1 { public static void main(String[] args) { System.out.println("程序开始"); method(); try { method1(); }catch(ArrayIndexOutOfBoundsException e) { e.printStackTrace(); } System.out.println("程序结束了哦哦哦"); } public static void method() { String s ="2021-05-01"; SimpleDateFormat sdf=new SimpleDateFormat("yyyy-mm-dd"); try { Date date = sdf.parse(s); }catch(ParseException r) { r.printStackTrace(); } } //谁调用就跟谁 public static void method1() throws ArrayIndexOutOfBoundsException{ int [] arr =new int [3]; System.out.println(arr[3]); } }
package collection; import java.util.*; public class CollectionDemo2 { public static void main(String[] args) { //创建一个集合对象 Collectionc = new ArrayList (); //创建3个学生对象 Student s1 = new Student("刘德华",55); Student s2 = new Student("张学友",58); Student s3 = new Student("黎明",52); Student s4 =new Student("自学",23); Student s5 =new Student("限为",203); //将3个学生对象添加到集合当中 c.add(s1); c.add(s2); c.add(s3); c.add(s4); c.add(s5); System.out.println("采用迭代器遍历集合的元素"); //采用迭代器遍历集合的元素 Iterator it = c.iterator(); while(it.hasNext()){ Student s = it.next(); System.out.println("学生的姓名:"+s.getName()+",学生的年龄:"+s.getAge()); } } }
@4for循环遍历的三中方式.
package for1; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import list.Student; public class Forday { public static void main(String[] args) { int [] arr = {12,34,56,7,78,9233,33333}; //第一种方法 for (int i = 0; i < arr.length; i++) { System.out.println(arr[i]); } System.out.println("----------------------------"); //第二种方法 //for(数据类型 变量名:集合/数组){System.out.println();} for(int a:arr) { System.out.println(a); } System.out.println("============================================================================================"); //2 List@5linkedList集合。c =new ArrayList (); //创建一个list的集合对象 Student s1 =new Student("小额",23); Student s2 =new Student("小王",73); Student s3 =new Student("小红",103); Student s4 =new Student("小q",23); Student s5 =new Student("小",33); c.add(s1); c.add(s2); c.add(s3); c.add(s4); c.add(s5); //第一种方法 System.out.println("11111111111111111111111111111111"); Iterator it =c.iterator(); while(it.hasNext()) { Student s=it.next(); System.out.println("学生姓名:"+s.getName()+",学生年龄:"+s.getAge()); } System.out.println("===============222222222222222222========================================="); //第二种方法 for (Student student : c) { System.out.println(student); } System.out.println("===================3333333333333==========================================="); //第三种方式 for (int j = 0; j < c.size(); j++) { System.out.println(c); } } }
package linkedList; import java.util.linkedList; import java.util.List; public class linkedListTest{ public static@6Set的集合- void main(String[] args) { linkedList
lList = new linkedList (); lList.add("stu"); lList.add("stu1"); lList.add("stu2"); lList.add("stu3"); lList.add("stu4"); lList.add("stu5"); lList.add("stu6"); lList.add("stu7"); lList.add("stu8"); lList.add("stu9"); System.out.println("链表的第一个元素是 : " + lList.getFirst()); System.out.println("链表最后一个元素是 : " + lList.getLast()); for (String str: lList) { System.out.println(str); } List subl = lList.subList(1, 6); System.out.println(subl); lList.remove(2); System.out.println(lList); System.out.println(lList); } }
package Setday; import java.util.HashSet; import java.util.Set; public class Setday { private static char[] str; public static void main(String[] args) { Set@8Lists=new HashSet (); s.add("a"); s.add("ab"); s.add("abc"); s.add("abcd"); for (String string : s) { System.out.println(s); } System.out.println("------------------------------------"); for (int i = 0; i < s.size(); i++) { System.out.println(s); } } }
import java.util.ArrayList; import java.util.Iterator; import java.util.Set; import java.util.TreeSet; public class List { public static void main(String[] args) { //list集合遍历 ArrayListlist=new ArrayList (); list.add("heiiow 嗯嗯word do you"); list.add("heiiow like 好了do you"); list.add("hei口口iow love do you"); list.add("heiiow mayn买空卖空t do you"); list.add("heiiow ma黄金国际p do you"); list.add("hei浏览器登录iow list do you"); //第一种遍历 for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); } System.out.println("-------------1111------------------"); //第二种遍历 Iterator it =list.iterator(); while(it.hasNext()) { System.out.println(it.next()); } System.out.println("==========2222===================="); //第三种 for (String l : list) { System.out.println(l); } System.out.println("===========33333===================="); System.out.println("-------------------------------"); //第四种 for (Object Object: list) { System.out.println(Object); } } }
上面的错误如何解决。代码如下。@9 Hashset。package com.chengji.paixun; import java.util.TreeSet; public class StudentTest01 { public static void main(String[] args) { //创建TreeSet集合对象,通过比较器排序进行排序 TreeSetts = new TreeSet (); //创建学生对象 Student s1 = new Student("林青霞", 98, 100); Student s2 = new Student("张曼玉", 95, 95); Student s3 = new Student("王祖贤", 100, 93); Student s4 = new Student("柳岩", 100, 97); Student s5 = new Student("风清扬", 98, 98); Student s6 = new Student("左冷禅", 97, 99); // Student s7 = new Student("左冷禅", 97, 99); Student s7 = new Student("赵云", 97, 99); //把学生对象添加到集合 ts.add(s1); ts.add(s2); ts.add(s3); ts.add(s4); ts.add(s5); ts.add(s6); ts.add(s7); //遍历集合 for (Student s : ts) { System.out.println(s.getName() + "," + s.getChinese() + "," + s.getMath() + "," + s.getSum()); } } } package com.chengji.paixun; public class Student { private String name; private int chinese; private int math; public Student() { } public Student(String name, int chinese, int math) { this.name = name; this.chinese = chinese; this.math = math; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getChinese() { return chinese; } public void setChinese(int chinese) { this.chinese = chinese; } public int getMath() { return math; } public void setMath(int math) { this.math = math; } public int getSum() { return this.chinese + this.math; } }
package com.set; public class HashsetText1 { public static void main(String[] args) { //创建学生对象 Student stu =new Student("xangxang",34); System.out.println(stu); System.out.println(stu.hashCode()); System.out.println("==============================="); Student stu1 =new Student("tyuop",34); System.out.println(stu1); System.out.println(stu1.hashCode()); System.out.println("================================"); //查看哈希值二个对象 Student stu2 =new Student("maomao",37); System.out.println(stu2); System.out.println(stu2.hashCode()); //查看哈希值三个对象 Student stu3 =new Student("nopains nogains",34); System.out.println(stu3); System.out.println(stu3.hashCode()); //查看哈希值四个对象 Student stu4 =new Student("nopains nogains",34); System.out.println(stu4); System.out.println(stu4.hashCode()); System.out.println("================================"); //查看哈希值五个对象 Student stu5 =new Student("nopains nogains",34); System.out.println(stu5); System.out.println(stu5.hashCode()); //查看哈希值六个对象 Student stu6 =new Student("nopains nogains",34); System.out.println(stu6); System.out.println(stu6.hashCode()); System.out.println(stu6.hashCode()); } }
package com.set; public class HashsetText2 { public static void main(String[] args) { //创建学生对象 Student stu =new Student("小小程序员",34); System.out.println(stu); System.out.println(stu.hashCode()); System.out.println("==============================="); Student stu1 =new Student("com on",34); System.out.println(stu1); System.out.println(stu1.hashCode()); System.out.println("================================"); //查看哈希值二个对象 Student stu2 =new Student("毛毛非",37); System.out.println(stu2); System.out.println(stu2.hashCode()); //查看哈希值三个对象 Student stu3 =new Student("星星胡i11nogains",34); System.out.println(stu3); System.out.println(stu3.hashCode()); //查看哈希值四个对象 Student stu4 =new Student("nopains nogains",34); System.out.println(stu4); System.out.println(stu4.hashCode()); System.out.println("================================"); //查看哈希值五个对象 Student stu5 =new Student("nopains nogains",34); System.out.println(stu5); System.out.println(stu5.hashCode()); //查看哈希值六个对象 Student stu6 =new Student("nopains nogains",34); System.out.println(stu6); System.out.println(stu6.hashCode()); System.out.println(stu6.hashCode()); } }
package com.set; import java.util.HashSet; import java.util.Iterator; import java.util.Set; public class Set2 { public static void main(String[] args) { Sets=new HashSet (); s.add("今天天青"); s.add("行万里路"); s.add("加强学习java"); s.add("自强不息"); s.add("好好学习"); for (int i = 0; i @10Map package Map; import java.util.*; public class HashMapDemo4 { private static final String HashMap = null; public static void main(String[] args) { HashMap@11项目实战,hmap =new HashMap (); hmap.put("name", "机熊"); hmap.put("sex", "nan"); hmap.put("id", "2345689"); hmap.put("height", "145"); hmap.put("age", "1003"); //第一种方法keyset for(String key:hmap.keySet()) { System.out.println(key+"-----"+hmap.get(key)); } System.out.println("====================================="); //第二种方法 for(Map.Entry entry:hmap.entrySet()) { System.out.println(entry); } System.out.println("========"); for(String key :hmap.keySet()) { System.out.println("key===="+key); } System.out.println("///"); for(String value :hmap.values()) { System.out.println("value===="+value); } System.out.println("333333333333333"); //keyset Iterator iterator=hmap.keySet().iterator(); while(iterator.hasNext()) { String key=iterator.next(); System.out.println(key+" : "+hmap.get(key)); } //entryset Iterator > iterator1=hmap.entrySet().iterator(); while(iterator.hasNext()) { Map.Entry entry= iterator1.next(); System.out.println(entry.getKey()); } } } package com.web1; import java.util.ArrayList; import java.util.Scanner; public class Reading { public static void main(String[] args) { // TODO Auto-generated method stub ArrayListarray=new ArrayList (); while(true){ System.out.println("--------欢迎来到学生图书管理系统 按以下执行 "); System.out.println("1 添 加 学 生喜爱的图书 "); System.out.println("2删 除 学 生不喜欢的数 "); System.out.println("3 修 改 学 生 图书编号"); System.out.println("4 查看所有学生信息"); System.out.println("5 退 出 "); System.out.println("请输入你的要选择的编号:"); Scanner sc=new Scanner(System.in); String line=sc.nextLine(); switch(line){ case "1": addStudent(array);break; case "2": deleteStudent(array);break; case"3": updateStudent(array);break; case"4": findAllStudent(array);break; //JVM退出 } } } public static void addStudent(ArrayList array ){ String sid; while(true){ System.out.println("请输入学生学号:"); Scanner sc=new Scanner(System.in); sid =sc.nextLine(); boolean flag=isUsed(array,sid); if(flag){ System.out.println("你输入的学号已经被占用,请重新输入"); }else{ break; } } Scanner sc=new Scanner(System.in); System.out.println("请输入学生姓名:"); String name = sc.nextLine(); System.out.println("请输入学生年龄:"); String age = sc.nextLine(); System.out.println("请输入学生居住地:"); String address = sc.nextLine(); Student s =new Student(); s.setAge(age); s.setSid(sid); s.setName(name); s.setAddress(address); array.add(s); System.out.println("添加学生成功"); } public static boolean isUsed(ArrayList array, String sid) { //如果与集合中的某一个学生学号相同,返回true;如果都不相同,返回false boolean flag = false; for(int i=0; i array) { //判断集合中是否有数据,如果没有显示提示信息 if (array.size() == 0) { System.out.println("无信息,请先添加信息再查询"); //为了让程序不再往下执行,我们在这里写上return; return; } //显示表头信息 //t其实是一个tab键的位置 System.out.println("学号ttt姓名tt年龄tt居住地"); //将集合中数据取出按照对应格式显示学生信息,年龄显示补充“岁” for (int i = 0; i < array.size(); i++) { Student s = array.get(i); System.out.println(s.getSid() + "t" + s.getName() + "t" + s.getAge() + "岁tt" + s.getAddress()); } } //定义一个方法,用于删除学生信息 public static void deleteStudent(ArrayList array) { //键盘录入要删除的学生学号,显示提示信息 Scanner sc = new Scanner(System.in); System.out.println("请输入你要删除的学生的学号:"); String sid = sc.nextLine(); //在删除/修改学生 *** 作前,对学号是否存在进行判断 //如果不存在,显示提示信息 //如果存在,执行删除/修改 *** 作 int index = -1; for (int i = 0; i < array.size(); i++) { Student s = array.get(i); if (s.getSid().equals(sid)) { index = i; break; } } if (index == -1) { System.out.println("该信息不存在,请重新输入"); } else { array.remove(index); //给出删除成功提示System.out.println("删除学生成功"); } } //定义一个方法,用于修改学生信息 public static void updateStudent(ArrayList array) { //键盘录入要修改的学生学号,显示提示信息 Scanner sc = new Scanner(System.in); System.out.println("请输入你要修改的学生的学号:"); String sid = sc.nextLine(); //键盘录入要修改的学生信息 System.out.println("请输入学生新姓名:"); String name = sc.nextLine(); System.out.println("请输入学生新年龄:"); String age = sc.nextLine(); System.out.println("请输入学生新居住地:"); String address = sc.nextLine(); //创建学生对象 Student s = new Student(); s.setSid(sid); s.setName(name); s.setAge(age); s.setAddress(address); //遍历集合修改对应的学生信息 for (int i = 0; i < array.size(); i++) { Student student = array.get(i); if (student.getSid().equals(sid)) { array.set(i, s); } } //给出修改成功提示System.out.println("修改学生成功"); } } package com.web1; public class Student { public String sid; public String name; public String age; public String address ; public void Student(){} public Student(String sid, String name, String age, String address) { super(); this.sid = sid; this.name = name; this.age = age; this.address = address; } public Student() { super(); // TODO Auto-generated constructor stub } public String getSid() { return sid; } public void setSid(String sid) { this.sid = sid; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)