java中数组中存储对象
package com. solid. array;
public class Person {
private String firstName;
private String lastName;
private int age;
public Person ( String firstName, String lastName, int age) {
this . firstName = firstName;
this . lastName = lastName;
this . age = age;
}
public int getAge ( ) {
return age;
}
public void setAge ( int age) {
this . age = age
}
public String getFirstName ( ) {
return firstName;
}
public void setFirstName ( String firstName) {
this . firstName = firstName;
}
public String getLastName ( ) {
return lastName;
}
public void setLastName ( String lastName) {
this . lastName = lastName;
}
}
package com. solid. array;
public class ObjectArray {
private Person[ ] person;
private int nElems;
public ObjectArray ( int max) {
person = new Person [ max] ;
nElems = 0 ;
}
public void insert ( String firstName, String lastName, int age) {
person[ nElems] = new Person ( firstName, lastName, age) ;
nElems++ ;
}
public boolean delete ( String lastName) {
int i;
for ( i= 0 ; i< nElems; i++ ) {
if ( person[ i] . getLastName ( ) . equals ( lastName) )
break ;
}
if ( i == nElems) {
System. out. println ( "can't find: " + lastName) ;
return false ;
} else {
for ( int j= i; j< nElems; j++ ) {
person[ j] = person[ j+ 1 ] ;
}
nElems-- ;
System. out. println ( "delete success" ) ;
return true ;
}
}
public boolean find ( String lastName) {
int i;
for ( i= 0 ; i< nElems; i++ ) {
if ( person[ i] . getLastName ( ) . equals ( lastName) )
break ;
}
if ( i == nElems) {
System. out. println ( "can't find: " + lastName) ;
return false ;
} else {
System. out. println ( "find it" ) ;
return true ;
}
}
public void display ( ) {
for ( int i= 0 ; i< nElems; i++ ) {
System. out. println ( person[ i] . getLastName ( ) + person[ i] . getFirstName ( ) + ":" + person[ i] . getAge ( ) ) ;
}
}
————————————————
package com. solid. array;
public class Person {
private String firstName;
private String lastName;
private int age;
public Person ( String firstName, String lastName, int age) {
this . firstName = firstName;
this . lastName = lastName;
this . age = age;
}
public int getAge ( ) {
return age;
}
public void setAge ( int age) {
this . age = age;
}
public String getFirstName ( ) {
return firstName;
}
public void setFirstName ( String firstName) {
this . firstName = firstName;
}
public String getLastName ( ) {
return lastName;
}
public void setLastName ( String lastName) {
this . lastName = lastName;
}
}
package com. solid. array;
public class ObjectArray {
private Person[ ] person;
private int nElems;
public ObjectArray ( int max) {
person = new Person [ max] ;
nElems = 0 ;
}
public void insert ( String firstName, String lastName, int age) {
person[ nElems] = new Person ( firstName, lastName, age) ;
nElems++ ;
}
public boolean delete ( String lastName) {
int i;
for ( i= 0 ; i< nElems; i++ ) {
if ( person[ i] . getLastName ( ) . equals ( lastName) )
break ;
}
if ( i == nElems) {
System. out. println ( "can't find: " + lastName) ;
return false ;
} else {
for ( int j= i; j< nElems; j++ ) {
person[ j] = person[ j+ 1 ] ;
}
nElems-- ;
System. out. println ( "delete success" ) ;
return true ;
}
}
public boolean find ( String lastName) {
int i;
for ( i= 0 ; i< nElems; i++ ) {
if ( person[ i] . getLastName ( ) . equals ( lastName) )
break ;
}
if ( i == nElems) {
System. out. println ( "can't find: " + lastName) ;
return false ;
} else {
System. out. println ( "find it" ) ;
return true ;
}
}
public void display ( ) {
for ( int i= 0 ; i< nElems; i++ ) {
System. out. println ( person[ i] . getLastName ( ) + person[ i] . getFirstName ( ) + ":" + person[ i] . getAge ( ) ) ;
}
}