![]()
package 对象数组;
public class Student {
private int id;
private String name;
private int age;
package 对象数组;
import java.util.Scanner;
public class StudentTest2 {
public static void main(String[] args) {
// 1、创建数组存储对象。
Student[] s = new Student[3];
Scanner sc = new Scanner(System.in);
// 2、创建学生对象
Student s1 = new Student(1, "cqz", 18);
Student s2 = new Student(2, "xyq", 18);
Student s3 = new Student(3, "qian", 20);
// 3、学生对象存入数组
s[0] = s1;
s[1] = s2;
s[2] = s3;
// 查询id的学生,如果存在,他的年龄增加1;
int flag = 0;
int id = sc.nextInt();
for (int i = 0; i < s.length; i++) {
if (s[i] != null) {
if (s[i].getId() == id) {
int age = s[i].getAge()+1;
s[i].setAge(age);
flag = 1;
break;
}
}
}
if (flag == 0) {
System.out.println("不存在此ID.无法对年龄进行更改");
}
for (int i = 0; i < s.length; i++) {
if (s[i] != null) {
System.out.println(s[i].getId() + " " + s[i].getName() + " " + s[i].getAge());
}
}
}
}
package 对象数组;
import java.util.Scanner;
public class StudentTest1 {
public static void main(String[] args) {
// 1、创建数组存储对象。
Student[] s = new Student[3];
Scanner sc = new Scanner(System.in);
// 2、创建学生对象
Student s1 = new Student(001, "cqz", 18);
Student s2 = new Student(002, "xyq", 18);
Student s3 = new Student(003, "qian", 20);
// 3、学生对象存入数组
s[0] = s1;
s[1] = s2;
s[2] = s3;
//查询id的学生,如果相同,删除此人信息。
int flag = 0;
int id = sc.nextInt();
for (int i = 0; i < s.length; i++) {
if (s[i] != null) {
if (s[i].getId() == id) {
s[i] = null;
flag = 1;
break;
}
}
}
if (flag == 0) {
System.out.println("不存在此0ID.删除失败");
}
for (int i = 0; i < s.length; i++) {
if (s[i] != null) {
System.out.println(s[i].getId() + " " + s[i].getName() + " " + s[i].getAge());
}
}
//查询id的学生,如果存在,他的年龄增加1;
}
}
package 对象数组;
import java.util.Scanner;
public class StudentTest {
public static void main(String[] args) {
// 1、创建数组存储对象。
Student[] s = new Student[3];
Scanner sc = new Scanner(System.in);
// 2、创建学生对象
Student s1 = new Student(001, "cqz", 18);
Student s2 = new Student(002, "xyq", 18);
Student s3 = new Student(003, "qian", 20);
// 3、学生对象存入数组
s[0] = s1;
s[1] = s2;
// s[2] = s3;
// 4、添加一个对象,添加时对学号唯一性进行判断
Student s4 = new Student(001, "hebuyu", 18);
// 对学号唯一性进行判断
boolean flag = contains(s, s4.getId());
if (flag) {
System.out.println("此学号已经存在");
} else {
// 对学生数组长度进行判断
int count = counts(s);
// 如果没有存满
if (count < s.length) {
s[count] = s4;
for (int i = 0; i < s.length; i++) {
if (s[i] != null) {
System.out.println(s[i].getId() + " " + s[i].getName() + " " + s[i].getAge());
}
}
}
// 如果存满,创建一个新的数组.
else {
Student[] newS = createNewArr(s, count);
newS[count] = s4;
for (int i = 0; i < newS.length; i++) {
System.out.println(newS[i].getId() + " " + newS[i].getName() + " " + newS[i].getAge());
}
}
}
}
public static Student[] createNewArr(Student s[], int count) {
Student[] newS = new Student[count + 1];
for (int i = 0; i < s.length; i++) {
newS[i] = s[i];
}
return newS;
}
public static int counts(Student s[]) {
int count = 0;
for (int i = 0; i < s.length; i++) {
if (s[i] != null) {
count++;
}
}
return count;
}
public static boolean contains(Student[] s, int id) {
for (int i = 0; i < s.length; i++) {
if (s[i] != null) {
if (s[i].getId() == id) {
return true;
}
}
}
return false;
}
}
public Student() {
}
public Student(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
}