欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

运用面向对象思维对数组的增删改查

程序员文章站 2022-06-19 16:02:07
创建animal类,添加动物属性,添加构造方法和方法public class Animal {public String type;publicString name;publicint age;publicint legnum; public Animal(String type,String name,int age,int legnum){ this.type=type; this.name=name; this.age= age; this.....
  1. 创建animal类,添加动物属性,添加构造方法和方法
public class Animal { public String type; public String name; public int age; public int legnum; public Animal(String type,String name,int age,int legnum){ this.type=type; this.name=name; this.age= age; this.legnum = legnum; } public String getinfo(){ return "动物种类:"+type+" 名字:"+name+" 岁数:"+age+" 腿的数量:"+legnum; } 

2.数组的增加

 Scanner input = new Scanner(System.in); Animal[] ani = new Animal[3]; ani[0] = new Animal("袋鼠", "花花", 2, 2); ani[1] = new Animal("河马", "憨憨", 1, 3); ani[2] = new Animal("*", "草草", 3, 4); // 数组的遍历 for (int i = 0; i < ani.length; i++) { System.out.println(ani[i].getinfo()); } // 数组的增加 System.out.println("请输入您需要增加的动物信息:"); String type1 = input.next(); String name1 = input.next(); int age1 = input.nextInt(); int legnum1 = input.nextInt(); Animal num = new Animal(type1, name1, age1, legnum1); ani = Arrays.copyOf(ani, ani.length + 1); ani[ani.length-1] = num; for (int i = 0; i < ani.length; i++) { System.out.println(ani[i].getinfo()); } 

3.数组的删除操作

int index = 0; System.out.println("请输入您需要删除的动物"); String type2 =input.next(); String name2 = input.next(); int age2 =input.nextInt(); int legnum2 = input.nextInt(); Animal num = new Animal(type2, name2, age2, legnum2); System.out.println(num.getinfo()); for (int i = 0; i < ani.length;i++) { if (ani[i].type.equals(num.type) && ani[i].name.equals(num.name) && ani[i].age==num.age &&ani[i].legnum==num.legnum) { index = i; break; } } System.out.println(index); if(index==-1){ System.out.println("动物园内不存在该动物"); } else{ for(int j=index;j<ani.length-1;j++){ ani[j]=ani[j+1]; } } ani[ani.length-1]=new Animal(null,null,0,0); Arrays.copyOf(ani,ani.length-1); for (int i = 0; i < ani.length; i++) { System.out.println(ani[i].getinfo()); } 

4.数组的查找操作

int index = 0; System.out.println("请输入您需要查找的动物"); String type2 =input.next(); String name2 = input.next(); int age2 =input.nextInt(); int legnum2 = input.nextInt(); Animal num = newAnimal(type2, name2, age2, legnum2); System.out.println(num.getinfo()); for (int i = 0; i < ani.length;i++) { if (ani[i].type.equals(num.type) &&ani[i].name.equals(num.name) && ani[i].age == num.age && ani[i].legnum == num.legnum) { index = i; break; } } System.out.println("动物园内存在该动物,他所在的位置是:" + index); if (index == -1) { System.out.println("动物园内不存在该动物"); } 

5.数组的改操作

System.out.println("请输入您需要改变的动物类型:"); String type2 = input.next(); String name2 = input.next(); int age2 = input.nextInt(); int legnum2= input.nextInt(); Animal num = new Animal(type2, name2, age2,legnum2); System.out.println(num.getinfo()); for (int i = 0; i < ani.length; i++) { if (ani[i].type.equals(num.type) &&ani[i].name.equals(num.name) && ani[i].age == num.age && ani[i].legnum == num.legnum) { System.out.println("请输入新的动物:"); Stringtype3 = input.next(); String name3 = input.next(); int age3 =input.nextInt(); int legnum3 = input.nextInt(); Animal num1 = newAnimal(type3, name3, age3, legnum3); ani[i]= num1; } } for (int i =0; i < ani.length; i++) { System.out.println(ani[i].getinfo()); } 

本文地址:https://blog.csdn.net/qq_45960200/article/details/107409915