目錄
一、什么是對象數(shù)組?
二、對象數(shù)組的作用:
?三、對象數(shù)組的語法定義及動靜初始化:
語法定義:
靜態(tài)初始化:在定義數(shù)組的同時對數(shù)組元素進行初始化
?動態(tài)初始化:使用運算符new為數(shù)組分配空間
?四、對象數(shù)組案例演示:
案例需求:
?具體實現(xiàn)代碼:
一、什么是對象數(shù)組?
解析:1.顧名思義就是當(dāng)數(shù)組元素是類對象時,這樣的數(shù)組稱之為對象數(shù)組,在這種情況下,數(shù)組的每一個元素都是一個對象的引用。
2.對象數(shù)組,就是指包含了一組相關(guān)的對象。
二、對象數(shù)組的作用:
?解析:1.數(shù)組就是一個容器,當(dāng)創(chuàng)建了多個相同的類對象時,并且需要將這些類對象儲存時,這個時候?qū)ο髷?shù)組的作用就體現(xiàn)出來了。
2.通俗的說就是用來存多個對象的數(shù)組,將對象看成一個個數(shù)組元素
?三、對象數(shù)組的語法定義及動靜初始化:
-
語法定義:
一、先定義,再開辟數(shù)組空間
類名稱 對象數(shù)組名[] = null;
對象數(shù)組名 = new 類名稱[長度];
二、定義并開辟數(shù)組空間
類名稱 對象數(shù)組名[] = new 類名稱[長度];
?特別注意:在聲明對象數(shù)組后 ,必須對每個數(shù)組成員進行實例化之后才能直接使用,否則報空指針異常!
-
靜態(tài)初始化:在定義數(shù)組的同時對數(shù)組元素進行初始化
Student student[] = {new Student("小王",20),new Student("小明",20),new Student("小紅",20)};
// 這代表初始化了3個數(shù)組對象,分別是student[0],student[1],student[2]。
// 其中的"小王",20表示為對象的屬性值
-
?動態(tài)初始化:使用運算符new為數(shù)組分配空間
1.Student student[] = new Student[4];
// 這代表給student[]對象數(shù)組分配的長度為4的空間,并沒有對數(shù)組的元素進行初始化。即數(shù)組元素都為空。
2.// 初始化student對象數(shù)組,否則會空值針異常
for(int i = 0;i<student.length;i++){
// 由用戶自己動態(tài)決定輸入數(shù)據(jù)
String id =?scanner.next();
String name = scanner.next();
int age = scanner.nextInt();
student[i] = new Student(id, name, age);
}
?四、對象數(shù)組案例演示:
-
案例需求:
?案例分析實現(xiàn):
1.首先讀題可知我們需要抽離一個學(xué)生類,并在此類中創(chuàng)建學(xué)號,姓名,年齡三個屬性
2.按照要求1,2,定義一個add()添加方法,具體實現(xiàn)分析看如下代碼
3.按照要求3,4,定義一個delete()刪除方法,具體實現(xiàn)分析看如下代碼
4.按照要求5,定義一個select()查詢方法,具體實現(xiàn)分析看如下代碼
5.最后在Test3測試類中進行測試。
-
?具體實現(xiàn)代碼:
Student學(xué)生類代碼:
package com.feisi.work11;
/**
* @author 14491
* @version 1.0.0
* @description TODO
* @date 2022/7/21 19:06
*/
public class Student {
String id;
String name;
int age;
public Student() {
}
public Student(String id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
}
Test3測試類:
package com.feisi.work11;
import java.util.Scanner;
/**
* @author 14491
* @version 1.0.0
* @description TODO
* @date 2022/7/21 19:28
*/
public class Test3 {
public static void main(String[] args) {
// 創(chuàng)建對象
Student student1 = new Student("heima001", "小明", 18);
Student student2 = new Student("heima002", "小紅", 19);
Student student3 = new Student("heima003", "小花", 20);
// 對象數(shù)組
Student student[] = {student1, student2, student3};
/* //接收并創(chuàng)建新對象數(shù)組
Student student4[] = add(student);
// 調(diào)用刪除方法
delet(student4);*/
//調(diào)用查詢方法
select(student);
}
// 添加方法
public static Student[] add(Student student[]) {
// 新對象數(shù)組
Student student1[] = new Student[student.length + 1];
// 初始化新對象數(shù)組
for (int i = 0; i < student1.length; i++) {
String id = "0";
String name = "0";
int age = 0;
// 給每個屬性賦值
student1[i] = new Student(id, name, age);
}
// 數(shù)組索引
int index = 0;
System.out.println("請輸入學(xué)生的學(xué)號,姓名,年齡");
Scanner scanner = new Scanner(System.in);
Student student4 = new Student();
// 賦值
student4.id = scanner.next();
student4.name = scanner.next();
student4.age = scanner.nextInt();
// 遍歷數(shù)組
for (int i = 0; i < student1.length; i++) {
if (student[index].id.equals(student4.id)) {
System.out.println("該學(xué)號以存在,添加失敗?。。?);
break;
}
if (i == student.length) {
student1[i] = student4;
for (int j = 0; j < student1.length; j++) {
System.out.println(student1[j].id);
System.out.println(student1[j].name);
System.out.println(student1[j].age);
System.out.println("---------------");
}
} else {
student1[i] = student[index];
}
if (index != 2) {
index++;
}
}
return student1;
}
// 刪除方法
public static void delet(Student student[]) {
// 創(chuàng)建對象
Scanner scanner = new Scanner(System.in);
System.out.println("請輸入要刪除學(xué)生的id");
String id = scanner.next();
// 刪除
for (int i = 0; i < student.length; i++) {
if (student[i].id.equals(id)) {
student[i].id = "0";
student[i].name = "null";
student[i].age = 0;
System.out.println("以刪除該學(xué)生信息?。。?);
for (int j = 0; j < student.length; j++) {
System.out.println(student[j].id);
System.out.println(student[j].name);
System.out.println(student[j].age);
System.out.println("-------------");
}
break;
} else if (i == student.length-1) {
System.out.println("刪除失?。。?!");
}
}
}
// 查詢方法
public static void select(Student student[]) {
// 創(chuàng)建對象
Scanner scanner = new Scanner(System.in);
System.out.println("請輸入要查詢學(xué)生的id");
String id = scanner.next();
// 遍歷對象數(shù)組
for (int i = 0; i < student.length; i++) {
if (student[i].id.equals(id)) {
System.out.println("修改前的age為:"+student[i].age);
student[i].age += 1;
System.out.println("修改后的age為:"+student[i].age);
break;
}else if(i== student.length-1){
System.out.println("無此學(xué)生id");
}
}
}
}
運行結(jié)果如下:
?
?文章來源:http://www.zghlxwxcb.cn/news/detail-404125.html
?文章來源地址http://www.zghlxwxcb.cn/news/detail-404125.html
到了這里,關(guān)于Day22_7 Java學(xué)習(xí)之對象數(shù)組的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!