[英]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.