Java基礎(chǔ)篇之對象數(shù)組練習(xí)
商品對象信息獲取
題目要求是這樣的:
定義數(shù)組存儲3個商品對象。
商品的屬性:商品的id,名字,價格,庫存。
創(chuàng)建三個商品對象,并把商品對象存入到數(shù)組當(dāng)中。
創(chuàng)建Goods類封裝商品信息類管理商品數(shù)據(jù),里面有商品的私有信息,以及獲取和設(shè)置成員變量的值的方法,通過調(diào)用Goods對象中的方法來對數(shù)據(jù)進行增刪改查操作:
public class Goods { private String id; private String name; private double price; private int count; // 創(chuàng)建構(gòu)造函數(shù) public Goods(String id, String name, double price, int count) { this.id = id; this.name = name; this.price = price; this.count = count; } // 創(chuàng)建getter和setter public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public int getCount() { return count; } public void setCount(int count) { this.count = count; } }
新創(chuàng)建一個Java類,這個類包含了一個main()方法,該方法用于測試Goods類的功能。在 main()方法中,首先創(chuàng)建了一個 Goods 類型的數(shù)組 arr,并且創(chuàng)建了三個 Goods 對象 g1、g2 和 g3,分別表示三種手機商品。然后,將這三個商品對象添加到數(shù)組中。最后,通過循環(huán)遍歷數(shù)組,輸出每個商品對象的信息(id、name、price、count):
public class GoodsTest { public static void main(String[] args) { // 創(chuàng)建一個數(shù)組 Goods[] arr = new Goods[3]; // 創(chuàng)建三個商品對象 Goods g1 = new Goods("001", "華為手機", 8000, 5); Goods g2 = new Goods("002", "蘋果手機", 11000.0, 7); Goods g3 = new Goods("003", "安卓手機", 6000.9, 1); // 將商品添加到數(shù)組當(dāng)中 arr[0] = g1; arr[1] = g2; arr[2] = g3; // 遍歷數(shù)組 for (int i = 0; i < arr.length; i++) { Goods goods = arr[i]; System.out.println(goods.getId()+", "+goods.getName()+", "+goods.getPrice()+", "+goods.getCount()); } } }
最終呈現(xiàn)的效果如下:
商品對象信息輸入
題目要求是這樣的:
定義數(shù)組存儲3部汽車對象。
汽車的屬性:品牌,價格,顏色。
創(chuàng)建三個汽車對象,數(shù)據(jù)通過鍵盤錄入而來,并把數(shù)據(jù)存入到數(shù)組當(dāng)中。
創(chuàng)建Car類封裝汽車信息類管理汽車數(shù)據(jù),里面有汽車的私有信息,以及獲取和設(shè)置成員變量的值的方法,通過調(diào)用Car對象中的方法來對數(shù)據(jù)進行增刪改查操作:
public class Car { private String brand; private int price; private String color; // 創(chuàng)建構(gòu)造函數(shù) public Car() {} public Car(String brand, int price, String color) { this.brand = brand; this.price = price; this.color = color; } // 創(chuàng)建getter和setter方法 public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = brand; } public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } }
創(chuàng)建了一個名為CarTest的Java類。這個類用于測試Car類的功能。首先創(chuàng)建了一個 Car 類型的數(shù)組 arr,用來存儲三個汽車對象。然后使用 Scanner 類從鍵盤輸入汽車的品牌、價格和顏色,并將這些信息設(shè)置到每個汽車對象中。接著,將每個汽車對象添加到數(shù)組中。最后,通過循環(huán)遍歷數(shù)組,輸出每個汽車對象的品牌、價格和顏色。
import java.util.Scanner; public class CarTest { public static void main(String[] args) { // 創(chuàng)建一個數(shù)組用來存儲3個汽車對象 Car[] arr = new Car[3]; // 創(chuàng)建汽車對象,數(shù)據(jù)鍵盤錄入 Scanner sc = new Scanner(System.in); for (int i = 0; i < arr.length; i++) { // 創(chuàng)建空的汽車對象 Car c = new Car(); // 錄入品牌 System.out.println("請輸入汽車的品牌"); String brand = sc.next(); c.setBrand(brand); // 間鍵盤錄入的數(shù)據(jù)寫入 // 錄入價格 System.out.println("請輸入汽車的價格"); int price = sc.nextInt(); c.setPrice(price); // 錄入顏色 System.out.println("請輸入汽車的顏色"); String color = sc.next(); c.setColor(color); // 把汽車對象添加到數(shù)組中 arr[i] = c; } // 遍歷數(shù)組 for (int i = 0; i < arr.length; i++) { Car car = arr[i]; System.out.println(car.getBrand()+", "+car.getPrice()+", "+car.getColor()); } } }
最終呈現(xiàn)的效果如下:
商品對象信息計算
題目要求是這樣的:
定義數(shù)組存儲3部手機對象。
手機的屬性:品牌,價格,顏色。
要求計算出三部手機的平均價格。
創(chuàng)建Phone類封裝手機信息類管理手機數(shù)據(jù),里面有手機的私有信息,以及獲取和設(shè)置成員變量的值的方法,通過調(diào)用Phone對象中的方法來對數(shù)據(jù)進行增刪改查操作:
public class Phone { private String brand; private int price; private String color; // 創(chuàng)建構(gòu)造函數(shù) public Phone() {} public Phone(String brand, int price, String color) { this.brand = brand; this.price = price; this.color = color; } // 創(chuàng)建getter和setter方法 public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = brand; } public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } }
創(chuàng)建了一個名為 PhoneTest 的 Java 類。這個類用于測試 Phone 類的功能。在 main() 方法中,首先創(chuàng)建了一個 Phone 類型的數(shù)組 arr,用來存儲三個手機對象。然后,通過使用 Phone 類的構(gòu)造函數(shù),創(chuàng)建了三個手機對象,并分別設(shè)置了品牌、價格和顏色。
接下來,將每個手機對象添加到數(shù)組中的對應(yīng)位置。然后,通過循環(huán)遍歷數(shù)組,計算所有手機的總價格,并將結(jié)果保存在變量 sum 中。最后,計算手機價格的平均值,并輸出結(jié)果。
public class PhoneTest { public static void main(String[] args) { // 創(chuàng)建一個數(shù)組 Phone[] arr = new Phone[3]; // 創(chuàng)建手機對象 Phone p1 = new Phone("小米", 4900, "黑色"); Phone p2 = new Phone("蘋果", 7900, "白色"); Phone p3 = new Phone("華為", 4200, "藍(lán)色"); // 將手機對象添加到數(shù)組當(dāng)中 arr[0] = p1; arr[1] = p2; arr[2] = p3; // 獲取三部手機的總價格 int sum = 0; for (int i = 0; i < arr.length; i++) { Phone phone = arr[i]; sum+= phone.getPrice(); } // 獲取平均值 int avg = sum / arr.length; System.out.println(avg); } }
最終呈現(xiàn)的效果如下:
商品對象信息統(tǒng)計
定義數(shù)組存儲4個電腦的對象
電腦的屬性:品牌、價格、顏色、種類
要求1:計算出4個電腦的平均價格
要求2:統(tǒng)計價格比平均值低的電腦有幾個?并把它們的所有信息打印出來。
創(chuàng)建Computer類封裝電腦信息類管理電腦數(shù)據(jù),里面有電腦的私有信息,以及獲取和設(shè)置成員變量的值的方法,通過調(diào)用Computer對象中的方法來對數(shù)據(jù)進行增刪改查操作:
public class Computer { private String brand; // 品牌 private double price; // 價格 private String color; // 顏色 private String type; // 種類 public Computer() { } public Computer(String brand, double price, String color, String type) { this.brand = brand; this.price = price; this.color = color; this.type = type; } public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = brand; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public String getType() { return type; } public void setType(String type) { this.type = type; } }
下面這段代碼主要展示了面向?qū)ο缶幊讨蓄惡蛯ο蟮氖褂?,以及?shù)組的操作和循環(huán)遍歷的應(yīng)用。通過這個例子可以更好地理解 Java 中的類和對象的概念,以及如何進行簡單的數(shù)據(jù)處理和統(tǒng)計分析:
public class ComputerTest { public static void main(String[] args) { // 定義數(shù)組存入電腦對象 Computer[] arr = new Computer[4]; // 創(chuàng)建電腦對象 Computer cp1 = new Computer("戴爾", 4900.0, "白色", "筆記本"); Computer cp2 = new Computer("神舟", 8200.0, "藍(lán)色色", "臺式"); Computer cp3 = new Computer("聯(lián)想", 6900.0, "黑色", "筆記本"); Computer cp4 = new Computer("機器人", 3900.0, "紅色", "臺式"); // 把對象添加到數(shù)組中 arr[0] = cp1; arr[1] = cp2; arr[2] = cp3; arr[3] = cp4; // 求和 double sum = 0; for (int i = 0; i < arr.length; i++) { Computer cp = arr[i]; sum += cp.getPrice(); } System.out.println("求和值為:"+ sum); // 平均值 double avg = sum / arr.length; System.out.println("平均值為:"+ avg); // 統(tǒng)計價格比平均值低的有幾個,并打印其信息 int count = 0; for (int i = 0; i < arr.length; i++) { Computer cp = arr[i]; if(cp.getPrice()<avg){ System.out.println(cp.getBrand()+", "+cp.getPrice()+", "+cp.getColor()+", "+cp.getType()); count++; } } System.out.println("低于平均價格的數(shù)量為:"+ count); } }
最終呈現(xiàn)的效果如下:
學(xué)生數(shù)據(jù)管理實現(xiàn)
定義數(shù)組存儲4個電腦的對象
定義一個長度為3的數(shù)組,數(shù)組存儲1~3名學(xué)生對象作為初始數(shù)據(jù),學(xué)生對象的學(xué)號,姓名各不相同,學(xué)生的屬性:學(xué)號,姓名,年齡。
要求1:再次添加一個學(xué)生對象,并在添加的時候進行學(xué)號的唯一性判斷。
要求2:添加完畢之后,遍歷所有學(xué)生信息。
要求3:通過id刪除學(xué)生信息,如果存在,則刪除,如果不存在,則提示刪除失敗。
要求4:刪除完畢之后,遍歷所有學(xué)生信息。
要求5:查詢數(shù)組id為“heima002”的學(xué)生,如果存在,則將他的年齡+1歲
創(chuàng)建Students類封裝學(xué)生信息類管理學(xué)生數(shù)據(jù),里面有學(xué)生的私有信息,以及獲取和設(shè)置成員變量的值的方法,通過調(diào)用Students對象中的方法來對數(shù)據(jù)進行增刪改查操作:
public class Students { private int id; private String name; private int age; public Students() { } public Students(int id, String name, int age) { this.id = id; this.name = name; this.age = age; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
下面這段代碼實現(xiàn)了對一個學(xué)生對象數(shù)組的增、刪、改、查操作,具體實現(xiàn)思路如下:
1)創(chuàng)建一個存儲學(xué)生對象的數(shù)組,長度為3:
Students[] arr = new Students[3];
2)創(chuàng)建三個學(xué)生對象,將它們添加到數(shù)組中:
Students stu1 = new Students(1,"zhangsan",20); Students stu2 = new Students(2,"lisi",35); Students stu3 = new Students(3,"wangwu",10); arr[0] = stu1; arr[1] = stu2; arr[2] = stu3;
3)添加一個新學(xué)生對象,并進行學(xué)號的唯一性判斷:
Students stu4 = new Students(4,"xiaoming",15); boolean flag = contains(arr, stu4.getId()); if (flag) { System.out.println("當(dāng)前id值重復(fù),請修改id后再進行添加"); } else { // 執(zhí)行添加操作 }
4)如果數(shù)組已滿,則創(chuàng)建一個新數(shù)組,否則直接在原數(shù)組上添加:
if(count == arr.length){ Students[] newArr = createNewArr(arr); newArr[count] = stu4; printArr(newArr); } else { arr[count] = stu4; printArr(arr); }
5)通過學(xué)號刪除一個學(xué)生對象,先找到該學(xué)生在數(shù)組中的位置,然后將其刪除:
int index = getIdIndex(newArr, 4); if (index >= 0) { if (arr.length < index){ arr[index] = null; printArr(arr); }else { newArr[index] = null; printArr(newArr); } } else { System.out.println("當(dāng)前的id不存在,刪除失敗"); }
6)修改一個學(xué)生對象的年齡,先找到該學(xué)生在數(shù)組中的位置,然后將其年齡加1:
int index1 = getIdIndex(newArr, 2); if (index1>0){ Students stu = arr[index1]; int newAge = stu.getAge() + 1; stu.setAge(newAge); printArr(newArr); }else { System.out.println("當(dāng)前的id不存在,修改失敗"); }
還有其他輔助方法包括:打印數(shù)組元素、創(chuàng)建新數(shù)組、獲取數(shù)組元素個數(shù)、判斷學(xué)號是否重復(fù)等??傮w思路是基于一個學(xué)生數(shù)組對象進行操作,對其增刪改查等多個操作都需要先找到該學(xué)生在數(shù)組中的索引位置,然后再進行相關(guān)操作。同時還需要注意學(xué)號唯一性等問題,具體代碼如下:
public class StudentsTest { public static void main(String[] args) { // 創(chuàng)建一個數(shù)組用來存儲學(xué)生對象 Students[] arr = new Students[3]; // 創(chuàng)建學(xué)生對象并添加到數(shù)組當(dāng)中 Students stu1 = new Students(1,"zhangsan",20); Students stu2 = new Students(2,"lisi",35); Students stu3 = new Students(3,"wangwu",10); // 把學(xué)生對象添加到數(shù)組當(dāng)中 arr[0] = stu1; arr[1] = stu2; arr[2] = stu3; // 再次添加一個學(xué)生對象,并在添加的時候進行學(xué)號的唯一性判斷 Students stu4 = new Students(4,"xiaoming",15); // 唯一性判斷 boolean flag = contains(arr, stu4.getId()); Students[] newArr = createNewArr(arr); if (flag) { // 數(shù)組元素已存在,不用添加 System.out.println("當(dāng)前id值重復(fù),請修改id后再進行添加"); }else { // 數(shù)組元素不存在,可以把學(xué)生對象添加進數(shù)組中 int count = getCount(arr); if(count == arr.length){ // 數(shù)組長度已經(jīng)存滿,創(chuàng)建一個新數(shù)組,長度=老數(shù)組的長度+1,然后把老數(shù)組的元素拷貝到新數(shù)組當(dāng)中 newArr[count] = stu4; // 添加完畢之后,遍歷所有的學(xué)生信息 System.out.println("-----當(dāng)前原數(shù)組元素為------"); printArr(newArr); } else { // 數(shù)組長度沒有存滿,假設(shè)數(shù)組已經(jīng)存了兩個 [stu1, stu2, null] // getCount獲取到的是2,表示數(shù)組當(dāng)中已經(jīng)有了2個元素,如果下一次要添加數(shù)據(jù),就是添加到數(shù)組長度的位置,即2索引,值就是count arr[count] = stu4; // 添加完畢之后,遍歷所有的學(xué)生信息 printArr(arr); System.out.println("-----當(dāng)前原數(shù)組元素為------"); } } // 通過id刪除學(xué)生信息 int index = getIdIndex(newArr, 4); if (index >= 0) { System.out.println("-----刪除完數(shù)組某元素為------"); if (arr.length < index){ arr[index] = null; printArr(arr); }else { newArr[index] = null; printArr(newArr); } }else { // 如果不存在,則表示刪除失敗 System.out.println("當(dāng)前的id不存在,刪除失敗"); } // 查詢數(shù)組id為2的學(xué)生,如果存在則將他的年齡+1 int index1 = getIdIndex(newArr, 2); if (index1>0){ // 存在 System.out.println("-----修改年齡后數(shù)組元素為------"); Students stu = arr[index1]; int newAge = stu.getAge() + 1; stu.setAge(newAge); printArr(newArr); } else { // 不存在直接顯示 System.out.println("當(dāng)前的id不存在,修改失敗"); } } // 找到id在數(shù)組當(dāng)中的索引 public static int getIdIndex(Students[] arr, int id) { for (int i = 0; i < arr.length; i++) { // 依次得到每一個學(xué)生對象 Students stu = arr[i]; // 對stu進行一個非空判斷 if (stu != null) { int sid = stu.getId(); if(sid == id){ return i; } } } return -1; } // 打印數(shù)組元素 public static void printArr(Students[] arr) { for (int i = 0; i < arr.length; i++) { Students stu = arr[i]; if (stu != null) { System.out.println(stu.getId()+", "+stu.getName()+", "+stu.getAge()); } } } // 創(chuàng)建新數(shù)組用于添加新元素 public static Students[] createNewArr(Students[] arr) { Students[] newArr = new Students[arr.length + 1]; // 循環(huán)遍歷老數(shù)組中的元素 for (int i = 0; i < arr.length; i++) { newArr[i] = arr[i]; } // 把新數(shù)組返回 return newArr; } // 定義一個方法判斷數(shù)組中已經(jīng)存在了幾個元素 public static int getCount(Students[] arr) { // 統(tǒng)計變量 int count = 0; for (int i = 0; i < arr.length; i++) { if (arr[i] != null){ count++; } } // 返回數(shù)組元素個數(shù) return count; } // 添加新數(shù)據(jù)到數(shù)組中進行學(xué)號的唯一性判斷 public static boolean contains(Students[] arr, int id) { // 依次獲取到數(shù)組里面的每一個學(xué)生對象 for (int i = 0; i < arr.length; i++) { // 依次獲取到數(shù)組里面的每一個學(xué)生對象 Students stu = arr[i]; if (stu != null){ // 獲取數(shù)組中學(xué)生對象的id int sid = stu.getId(); // 比較 if (sid == id) { return true; } } } // 當(dāng)循環(huán)結(jié)束之后,沒有找到id那么就表示數(shù)組中要查找的id是不存在的 return false; } }
得到的結(jié)果如下:
總結(jié)
到此這篇關(guān)于Java基礎(chǔ)篇之對象數(shù)組練習(xí)的文章就介紹到這了,更多相關(guān)Java對象數(shù)組練習(xí)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
spring的構(gòu)造函數(shù)注入屬性@ConstructorBinding用法
這篇文章主要介紹了關(guān)于spring的構(gòu)造函數(shù)注入屬性@ConstructorBinding用法,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12這一次搞懂Spring自定義標(biāo)簽以及注解解析原理說明
這篇文章主要介紹了這一次搞懂Spring自定義標(biāo)簽以及注解解析原理說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08spring session同域下單點登錄實現(xiàn)解析
這篇文章主要介紹了spring session同域下單點登錄實現(xiàn)解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-10-10spring cloud gateway中如何讀取請求參數(shù)
這篇文章主要介紹了spring cloud gateway中如何讀取請求參數(shù)的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07spring boot集成mongodb的增刪改查的示例代碼
這篇文章主要介紹了spring boot集成mongodb的增刪改查的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03SpringBoot?SpringSecurity?JWT實現(xiàn)系統(tǒng)安全策略詳解
Spring?Security是Spring的一個核心項目,它是一個功能強大且高度可定制的認(rèn)證和訪問控制框架。它提供了認(rèn)證和授權(quán)功能以及抵御常見的攻擊,它已經(jīng)成為保護基于spring的應(yīng)用程序的事實標(biāo)準(zhǔn)2022-11-11