package com.itheima.test7; public class Test { public static void main(String[] args) { //定义一个长度为三的数组 Student[] arr= new Student[3]; //添加数据 Student s1 = new Student(1,"lisa",25); Student s2 = new Student(2,"jisoo",26); //Student s3 = new Student(3,"rose",25); //赋值 arr[0] = s1; arr[1] = s2; //arr[2] = s3; //1.添加一个对象 Student s4 = new Student(4,"jennie",25); //唯一性判断,已存在,则不用添加 boolean flag = contains(arr,s4.getId()); if(flag){ System.out.println("id重复"); }else{ //数组已满,创建新数组,新数组长度=老数组+1; //数组没满,直接添加 写成方法 int count = getCount(arr); if(count== arr.length){ //已经存满,将老数组传递给新数组 Student[] newArr = createNewArr(arr); //count是填充新元素的索引 newArr[count] = s4; //2.遍历所有学生信息 printArr(newArr); }else{ arr[count] = s4; printArr(arr); } } } //打印元素,需要新数组老数组都打印所以要写方法,只需要打印,返回值为空 public static void printArr(Student[] arr){ for (int i = 0; i < arr.length ; i++) { //数组要用变量接收 Student stu = arr[i]; if(stu!=null){ System.out.println(stu.getId()+","+ stu.getAge()+","+stu.getName()); } } } //创建新数组,返回的是Student[]类型的数组 public static Student[] createNewArr(Student[] arr){ Student[] newArr =new Student[arr.length+1]; //循环遍历得到老数组中每一个元素 for (int i = 0; i < arr.length; i++) { newArr[i] = arr[i]; } return newArr; } //定义一个方法判断数组中存了几个元素 public static int getCount(Student[] arr){ //统计不为空的元素 int count= 0 ; for (int i = 0; i < arr.length; i++) { if(arr[i]!=null){ count++; } } return count; } //需要数组、id,需要判断是否存在,所以要返回,用boolean public static boolean contains(Student[] arr,int id){ for (int i = 0; i < arr.length; i++) { //获取数组里的每一个学生对象 Student stu = arr[i]; if(stu != null){ //null不能直接使用 //获取数组中学生对象的id int sid = stu.getId(); //比较 if (sid == id){ return true; } } } //循环结束后未找到直接判断错误 return false; } }
1 package com.itheima.test7; 2 3 public class Test3 { 4 public static void main(String[] args) { 5 //定义一个长度为三的数组 6 Student[] arr= new Student[3]; 7 //添加数据 8 Student s1 = new Student(1,"lisa",25); 9 Student s2 = new Student(2,"jisoo",26); 10 Student s3 = new Student(3,"rose",25); 11 //赋值 12 arr[0] = s1; 13 arr[1] = s2; 14 arr[2] = s3; 15 //3.通过id删除信息 16 //如果存在,则删除,如果不存在,则提示删除失败 17 int index = getIndex(arr, 4); 18 if(index>=0){ 19 //如果存在则删除(索引置空) 20 arr[index] = null; 21 //遍历数组 22 printArr(arr); 23 }else{ 24 System.out.println("id不存在,删除失败"); 25 } 26 } 27 28 //判断是否存在,所以必须返回 29 public static int getIndex(Student[] arr,int id){ 30 for (int i = 0; i < arr.length; i++) { 31 //依次获得每一个学生对象 32 Student stu = arr[i]; 33 //对stu进行一个非空判断 34 if(stu!=null){ 35 int sid = stu.getId(); 36 if(sid == id){ 37 return i; 38 } 39 } 40 41 } 42 //循环结束后还没找到 43 return -1; 44 } 45 //打印元素,需要新数组老数组都打印所以要写方法,只需要打印,返回值为空 46 public static void printArr(Student[] arr){ 47 for (int i = 0; i < arr.length ; i++) { 48 //数组要用变量接收 49 Student stu = arr[i]; 50 if(stu!=null){ 51 System.out.println(stu.getId()+","+ stu.getAge()+","+stu.getName()); 52 } 53 } 54 } 55 }
1 package com.itheima.test7; 2 3 public class Test4 { 4 public static void main(String[] args) { 5 6 Student[] arr= new Student[3]; 7 8 Student s1 = new Student(1,"lisa",25); 9 Student s2 = new Student(2,"jisoo",26); 10 Student s3 = new Student(3,"rose",25); 11 12 arr[0] = s1; 13 arr[1] = s2; 14 arr[2] = s3; 15 16 //id为2的年龄+1 17 int index =getIndex(arr,2); 18 19 //判断索引 20 if(index>=0){ 21 //存在年龄+1 22 Student s = arr[index]; 23 //把原来的年龄拿出来 24 int newAge = s.getAge() + 1; 25 //把+1之后的年龄塞回去 26 s.setAge(newAge); 27 printArr(arr); 28 }else { 29 System.out.println("不存在"); 30 } 31 } 32 //判断是否存在,所以必须返回 33 public static int getIndex(Student[] arr,int id) { 34 for (int i = 0; i < arr.length; i++) { 35 //依次获得每一个学生对象 36 Student stu = arr[i]; 37 //对stu进行一个非空判断 38 if (stu != null) { 39 int sid = stu.getId(); 40 if (sid == id) { 41 return i; 42 } 43 } 44 } 45 //循环结束后还没找到 46 return -1; 47 } 48 49 //打印元素,需要新数组老数组都打印所以要写方法,只需要打印,返回值为空 50 public static void printArr(Student[] arr){ 51 for (int i = 0; i < arr.length ; i++) { 52 //数组要用变量接收 53 Student stu = arr[i]; 54 if(stu!=null){ 55 System.out.println(stu.getId()+","+ stu.getAge()+","+stu.getName()); 56 } 57 } 58 } 59 }
1 package com.itheima.test7; 2 3 public class Student { 4 private int id; //对字符串操作 先用int 5 private String name; 6 private int age; 7 8 public Student() { 9 } 10 11 public Student(int id, String name, int age) { 12 this.id = id; 13 this.name = name; 14 this.age = age; 15 } 16 17 public int getId() { 18 return id; 19 } 20 21 public void setId(int id) { 22 this.id = id; 23 } 24 25 public String getName() { 26 return name; 27 } 28 29 public void setName(String name) { 30 this.name = name; 31 } 32 33 public int getAge() { 34 return age; 35 } 36 37 public void setAge(int age) { 38 this.age = age; 39 } 40 }
标签:arr,删除,stu,int,id,面向对象,添加,Student,public From: https://www.cnblogs.com/Wang0626/p/17232582.html