简体   繁体   中英

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;
}

 }

Let's say I want to access the price for c3 . In the same method I can just print it but I want to access the price for c3 on a main method in another class. Here is the code. Let's say that from the menu, the user enters 1 and decides to buy the first option. I got it to print the options, but I don't know how to access the price for it.

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());
            }
        }


}
}

Okay, so you can get the Cases from the array s class...

ArrayList cases = arrays.Case();

Next, you need to find out which case the user is interested in...

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());

Then you need to simple access the specified item from the 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());
}

Take a closer look at Collections for more details.

Suggestions

Beware that there is already a class called Arrays in the libraries, it might be a good idea to rename it to remove confusion.

In order to prevent the need to cast the element from the ArrayList , you should be providing a generic return result, for example...

Instead of public ArrayList Case() { , you should be using public ArrayList<Case> Case() { , this ensures that when you do cases.get , the compiler will expect a Case and means you can get away with doing something like Case case = cases.get(choice); which is much safer.

In Java it is recommended that Java method names start with a lowercase

public ArrayList<Case> case() {...
public ArrayList<Monitor> monitor() {...

It might also be appropriate to use get for methods that "get" stuff, for example...

public ArrayList<Case> getCases() {...
public ArrayList<Monitor> getMonitors() {...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM