[英]how to access a specific part of an ArrayList from one class to another
package HW1;
import java.util.ArrayList;
public class Arrays {
public ArrayList Case() {
ArrayList <Case> cases = new ArrayList<Case>();
Case c1 = new Case("Antec Twelve Hundred V3", 20.20, 8.39, 22.87, "ATX Full Tower", 154.99);
Case c3 = new Case("BitPhenix Phenom M Arctic", 9.80, 12.95, 14.69, "MicroATX Mini Tower", 89.99);
Case c2 = new Case("Corsair 250D", 13.8, 10.9, 11.4, "MiniITX Tower", 99.9);
cases.add(c1);
cases.add(c2);
cases.add(c3);
for (int i = 0; i < cases.size(); i++ ) {
System.out.println("Name: " + cases.get(i).getName() + "\n" + "Dimensions: " + cases.get(i).getLength() + "' x " + cases.get(i).getWidth() + "' x " + cases.get(i).getHeight() + "' " + "Type: " +cases.get(i).getType() + "\n" + "Price: $" + cases.get(i).getPrice()+ "\n");
}
return cases;
}
public ArrayList Monitor() {
ArrayList <Monitor> monitor = new ArrayList<Monitor>();
Monitor m1 = new Monitor("Asus PB278Q", 27, 549.99);
Monitor m2 = new Monitor("Dell U2412M", 24, 263.99);
Monitor m3 = new Monitor("Samsung S22300H", 21.5, 139.99);
monitor.add(m1);
monitor.add(m2);
monitor.add(m3);
for (int i = 0; i < monitor.size(); i++ ) {
System.out.println("Name: " + monitor.get(i).getName() + "\n" + "Size: " + monitor.get(i).getSize() + "' " + "\n" + "Price: $" + monitor.get(i).getPrice() + "\n");
}
return monitor;
}
public ArrayList CPU() {
ArrayList <CPU> cpu = new ArrayList<CPU>();
CPU cpu1 = new CPU("AMDFX-8350", 4.0, 188.99);
CPU cpu2 = new CPU("Intel Core i5-3570K", 3.4, 179.99);
CPU cpu3 = new CPU("Intel Core i7-3770K", 3.5, 249.99);
cpu.add(cpu1);
cpu.add(cpu2);
cpu.add(cpu3);
for (int i = 0; i < cpu.size(); i++ ) {
System.out.println("Name: " + cpu.get(i).getName() + "\n" + "Speed: " + cpu.get(i).getSpeed() + "GHz " + "\n" + "Price: $" + cpu.get(i).getPrice() + "\n");
}
return cpu;
}
public ArrayList VideoCard() {
ArrayList <VideoCard> videoCard = new ArrayList<VideoCard>();
VideoCard vd1 = new VideoCard("Nvidia GeForce GTX 770", 2, 346.99);
VideoCard vd2 = new VideoCard("Nvidia GeForce GTX 670", 2, 333.99);
VideoCard vd3 = new VideoCard("AMD Radeon HD 7950", 3, 399.99);
videoCard.add(vd1);
videoCard.add(vd2);
videoCard.add(vd3);
for (int i = 0; i < videoCard.size(); i++ ) {
System.out.println("Name: " + videoCard.get(i).getName() + "\n" + "Memory Size: " + videoCard.get(i).getMemorySize() + "GB " + "\n" + "Price: $" + videoCard.get(i).getPrice() + "\n");
}
return videoCard;
}
public ArrayList HardDrive() {
ArrayList <HardDrive> hardDrive = new ArrayList<HardDrive>();
HardDrive hd1 = new HardDrive("Samsung MZ-7PD256BW", 256, 199.99);
HardDrive hd2 = new HardDrive("Corsair CSSD-F240GBGT-BK", 240, 206.99);
HardDrive hd3 = new HardDrive("Crucial CT128M4SSD1", 128, 199.95);
hardDrive.add(hd1);
hardDrive.add(hd2);
hardDrive.add(hd3);
for (int i = 0; i < hardDrive.size(); i++ ) {
System.out.println("Name: " + hardDrive.get(i).getName() + "\n" + "Memory Size: " + hardDrive.get(i).getSize() + "GB " + "\n" + "Price: $" + hardDrive.get(i).getPrice()+ "\n");
}
return hardDrive;
}
public ArrayList Memory() {
ArrayList <Memory> memory = new ArrayList<Memory>();
Memory me1 = new Memory("Corsair Vengeance", 8, 69.99);
Memory me2 = new Memory("G.Skill Ares Series", 16, 149.99);
Memory me3 = new Memory("Kingston Black Series", 8, 79.99);
memory.add(me1);
memory.add(me2);
memory.add(me3);
for (int i = 0; i < memory.size(); i++ ) {
System.out.println("Name: " + memory.get(i).getName() + "\n" + "Memory Size: " + memory.get(i).getSize() + "GB " + "\n" + "Price: $" + memory.get(i).getPrice()+"\n");
}
return memory;
}
}
假設我想訪問c3
的價格。 在相同的方法中,我可以只打印它,但我想在另一個類中的主要方法上訪問c3
的價格。 這是代碼。 假設從菜單中,用戶輸入1
並決定購買第一個選項。 我用它來打印選項,但我不知道如何獲取它的價格。
package HW1;
import java.util.*;
public class Main {
public static void printMenu()
{
System.out.println("Menu");
System.out.println("1: Choose a Case");
System.out.println("2: Choose a Monitor");
System.out.println("3: Choose a CPU");
System.out.println("4: Choose a Video Card");
System.out.println("5: Choose a Hard Drive");
System.out.println("6: Choose a Memory");
System.out.println("7: Delete Current Computer");
System.out.println("8: Display Receipt");
System.out.println("9: Quit program");
}
public static void main(String[] args) {
printMenu();
System.out.println("please select and option");
Arrays arrays = new Arrays();
Scanner in = new Scanner(System.in);
int choice = in.nextInt();
if (choice == 1) {
ArrayList cases = arrays.Case();
System.out.println(cases);
System.out.println("please select a case");
if (choice == 1) {
System.out.println(c1.getPrice());
}
}
}
}
好的,所以您可以從array
的類中獲取Cases
...
ArrayList cases = arrays.Case();
接下來,您需要找出用戶對哪種情況感興趣......
int choice = -1;
do {
System.out.println("please select a case");
choice = in.nextInt();
if (choice >= cases.size()) {
System.out.println("Please select from 0-" + (cases.size() - 1));
}
} while (choice < cases.size());
然后你需要從ArrayList
簡單訪問指定的項
// Allow for a non-positive exit value...
if (choice >= 0) {
// I know, blind cast, bad idea...keep reading...
Case aCase = (Case)cases.get(choice);
System.out.println(aCase.getPrice());
}
仔細查看集合以獲取更多詳細信息。
建議
請注意,庫中已經有一個名為Arrays
的類,重命名它以消除混淆可能是個好主意。
為了防止需要從ArrayList
元素,您應該提供一個通用的返回結果,例如...
而不是public ArrayList Case() {
,你應該使用public ArrayList<Case> Case() {
,這確保當你做cases.get
,編譯器會期待一個Case
並且意味着你可以逃避做類似Case case = cases.get(choice);
事情Case case = cases.get(choice);
這更安全。
在 Java 中,建議 Java 方法名稱以小寫開頭
public ArrayList<Case> case() {...
public ArrayList<Monitor> monitor() {...
將get
用於“獲取”東西的方法也可能是合適的,例如......
public ArrayList<Case> getCases() {...
public ArrayList<Monitor> getMonitors() {...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.