题目
定义一个长度为3的数组,数组存储1~3名学生对象作为初始数据,学生对象的学号,姓名各不相同。学生的属性:学号,姓名,年龄。
要求1:再次添加一个学生对象,并在添加的时候进行学号的唯一性判断。
要求2: 添加完毕之后,遍历所有学生信息。
要求3:通过 id 删除学生信息
如果存在,则删除,如果不存在,则提示删除失败。
要求4:删除完毕之后,遍历所有学生信息。
要求5:查询数组 id 为“2"的学生,如果存在,则将他的年龄+1岁
分析
一、要求1
1.定义一个长度为3的数组,数组存储1~3名学生对象作为初始数据,
Student [ ] students = new Student [ 3 ] ;
2.创建一个Student类
public class Student {
private String name;
private int ID;
private int age;
public Student ( ) {
}
public Student ( String name, int ID, int age) {
this . name = name;
this . ID = ID;
this . age = age;
}
public String getName ( ) {
return name;
}
public void setName ( String name) {
this . name = name;
}
public int getID ( ) {
return ID;
}
public void setID ( int ID) {
this . ID = ID;
}
public int getAge ( ) {
return age;
}
public void setAge ( int age) {
this . age = age;
}
}
3.创建学生对象添加到数组中
Student stu1 = new Student ( "1" , 1 , 21 ) ;
Student stu2 = new Student ( "2" , 2 , 22 ) ;
Student stu3 = new Student ( "3" , 3 , 23 ) ;
students [ 0 ] = stu1;
students [ 1 ] = stu2;
students [ 2 ] = stu3;
4.再次添加一个学生对象
Student stu4 = new Student ( "4" , 4 , 25 )
5.并在添加的时候进行学号的唯一性判断。
要考虑到情况如果数组没有存满,只获取到null前面的元素
public static boolean contain ( Student [ ] arr, int id) {
for ( int i = 0 ; i < arr. length; i++ ) {
Student stu = arr[ i] ;
if ( stu != null ) {
int newid = stu. getID ( ) ;
if ( newid == id) {
return true ;
}
}
}
return false ;
}
6.进行函数的调用
boolean flag = contain ( students, 4 ) ;
if ( flag) {
System . out. println ( "学号重复" ) ;
} else {
System . out. println ( "学号不重复,可添加" ) ;
}
7.添加学号
注意:
如果初始创建的数组已满,那么就新建一个数组,将所有元素存放进去,
如果没有满,那么直接存入
下面就要判断数组中到底有没有存满,存了几个元素。
int count = getnumber ( students) ;
if ( count == students. length) {
} else {
students[ count] = stu4;
}
定义一个方法,得到数组中的元素个数
public static int getnumber ( Student [ ] arr) {
int count = 0 ;
for ( int i = 0 ; i < arr. length; i++ ) {
if ( arr[ i] != null ) {
count++ ;
}
}
return count;
}
8.如果没有存满,现在就写个方法定义一个新数组
public static Student [ ] newarr ( Student [ ] arr) {
Student [ ] arr1 = new Student [ arr. length+ 1 ] ;
for ( int i = 0 ; i < arr. length; i++ ) {
arr1[ i] = arr[ i] ;
}
return arr1;
}
9.然后进行函数的调用
if ( count == students. length) {
Student [ ] arr = newarr ( students) ;
arr[ count] = stu4;
二、要求2
1.添加完毕之后,遍历所有学生信息。
定义一个打印函数。
public static void print ( Student [ ] arr) {
for ( int i = 0 ; i < arr. length; i++ ) {
Student stu2 = arr[ i] ;
if ( stu2 != null ) {
System . out. println ( stu2. getName ( ) + "," + stu2. getAge ( ) + "," + stu2. getID ( ) ) ;
}
}
}
2.调用打印方法
if ( count == students. length) {
Student [ ] arr = newarr ( students) ;
arr[ count] = stu4;
print ( arr) ;
} else {
students[ count] = stu4;
print ( students) ;
}
小结
要求一 ,要求二结果
三、要求3、4
1.通过 id 删除学生信息,如果存在,则删除,如果不存在,则提示删除失败。
定义一个deleteId方法,寻找索引
public static int delete ( Student [ ] arr, int id) {
for ( int i = 0 ; i < arr. length; i++ ) {
Student stu = arr[ i] ;
if ( stu!= null ) {
int sid = stu. getID ( ) ;
if ( sid == id) {
return i;
}
}
}
return - 1 ;
}
2.调用deleteId方法
int deleteID = delete ( students, 4 ) ;
if ( deleteID>= 0 ) {
students[ deleteID] = null ;
print ( students) ;
} else {
System . out. println ( "删除失败" ) ;
}
三、要求5
查询数组 id 为“2"的学生,如果存在,则将他的年龄+1岁
先获得id为2的索引值,再让年龄加一即可
int Index = delete ( students, 2 ) ;
if ( Index >= 0 ) {
Student stu = students[ Index ] ;
int newage = stu. getAge ( ) + 1 ;
stu. setAge ( newage) ;
}
完整代码
public class text1 {
public static void main ( String [ ] args) {
Student [ ] students = new Student [ 3 ] ;
Student stu1 = new Student ( "1" , 1 , 21 ) ;
Student stu2 = new Student ( "2" , 2 , 22 ) ;
Student stu3 = new Student ( "3" , 3 , 23 ) ;
Student stu4 = new Student ( "4" , 4 , 25 ) ;
students [ 0 ] = stu1;
students [ 1 ] = stu2;
students [ 2 ] = stu3;
boolean flag = contain ( students, 4 ) ;
Student [ ] arr = newarr ( students) ;
if ( flag) {
System . out. println ( "学号重复" ) ;
} else {
System . out. println ( "学号不重复,可添加" ) ;
int count = getnumber ( students) ;
if ( count == students. length) {
arr[ count] = stu4;
print ( arr) ;
} else {
students[ count] = stu4;
print ( students) ;
}
}
int deleteID = delete ( students, 4 ) ;
if ( deleteID>= 0 ) {
students[ deleteID] = null ;
print ( students) ;
} else {
System . out. println ( "删除失败" ) ;
}
int Index = delete ( students, 1 ) ;
if ( Index >= 0 ) {
Student stu = students[ Index ] ;
int newage = stu. getAge ( ) + 1 ;
stu. setAge ( newage) ;
print ( arr) ;
} else {
System . out. println ( "找不到" ) ;
}
}
public static int delete ( Student [ ] arr, int id) {
for ( int i = 0 ; i < arr. length; i++ ) {
Student stu = arr[ i] ;
if ( stu!= null ) {
int sid = stu. getID ( ) ;
if ( sid == id) {
return i;
}
}
}
return - 1 ;
}
public static void print ( Student [ ] arr) {
for ( int i = 0 ; i < arr. length; i++ ) {
Student stu2 = arr[ i] ;
if ( stu2 != null ) {
System . out. println ( stu2. getName ( ) + "," + stu2. getAge ( ) + "," + stu2. getID ( ) ) ;
}
}
}
public static Student [ ] newarr ( Student [ ] arr) {
Student [ ] arr1 = new Student [ arr. length+ 1 ] ;
for ( int i = 0 ; i < arr. length; i++ ) {
arr1[ i] = arr[ i] ;
}
return arr1;
}
public static int getnumber ( Student [ ] arr) {
int count = 0 ;
for ( int i = 0 ; i < arr. length; i++ ) {
if ( arr[ i] != null ) {
count++ ;
}
}
return count;
}
public static boolean contain ( Student [ ] arr, int id) {
for ( int i = 0 ; i < arr. length; i++ ) {
Student stu = arr[ i] ;
if ( stu != null ) {
int newid = stu. getID ( ) ;
if ( newid == id) {
return true ;
}
}
}
return false ;
}
}