通过聚合修改通用数组列表

时间:2018-12-20 19:06:32

标签: java arraylist aggregate generic-collections

我制作了其中通用arrayList初始化为&的Product类 当我从主类中设置Laptop arraylist和Desktop ArrayList时出现问题,如果我添加了5台笔记本电脑,然后又添加了5台台式机,那么5台台式机在5台笔记本电脑上的位置将在第5个索引之后不再使用。在笔记本电脑数据完成后开始。当我通过该类中的getter方法打印“笔记本电脑ArrayList”和“桌面Arraylist”时,只会出现“桌面ArrayList”数据。

对不起,我的英语。如果您听不懂

import java.util.ArrayList;

public class Main {public static void main(String[] args) {
        Products P1 = P1=new Products();

        Laptop L1= new Laptop(1,"Dell","XPS","i7|2.8GHZ|8th Gen",95000);
        Laptop L2= new Laptop(2,"HP","Probook","i5|2.4GHZ|7th Gen",85000);
        Laptop L3= new Laptop(3,"Lenovo","Ideapad","i7|2.6 GHZ|7th Gen",80000);
        Laptop L4= new Laptop(4,"Sony","Vaio","i5|2.2 GHZ|7th Gen",90000);
        Laptop L5= new Laptop(5,"Asus","Zenbook","i3|2.6 GHZ|6th Gen",75000);

        Desktop D1 = new Desktop(7,"i5-2.8GHZ","6th Gen","1 TB",16,40000);
        Desktop D2 = new Desktop(8,"i7-2.6GHZ","7th Gen","2 TB",32,85000);
        Desktop D3 = new Desktop(9,"i5-8500","8th Gen","3 TB",16,90000);
        Desktop D4 = new Desktop(10,"i7-7800","7th Gen","1 TB",16,75000);
        Desktop D5 = new Desktop(11,"i3-8350","8th Gen","1 TB",32,65000);

        ArrayList<Laptop> laptop = new ArrayList<Laptop>();

        laptop.add(L1);
        laptop.add(L2);
        laptop.add(L3);
        laptop.add(L4);
        laptop.add(L5);
        laptop.add(L6);

        ArrayList<Desktop> desktop = new ArrayList<Desktop>();
        desktop.add(D1);
        desktop.add(D2);
        desktop.add(D3);
        desktop.add(D4);
        desktop.add(D5);        

        for(int i = 0; i<laptop.size(); i++){
            P1.setProduct(laptop);
        }
        for( int j=0;j<desktop.size();j++){
            P1.setProduct(desktop);
        }
        P1.specifyCategory();
    }
}

import java.util.ArrayList;
import java.util.Scanner;

public class Products<T> extends Category{
        private ArrayList<T> product;

    public Products() {
    }

    Products(ArrayList<T> product) {
        this.product.addAll(product);
    }

    void specifyCategory(){
        super.categories();
        Scanner input4 = new Scanner(System.in);
        System.out.println("\t\t\t\t\t\t\tChoose\n\t\t\t\t\t\t\t 1 for Laptop\n\t\t\t\t\t\t\t 2 for Desktop\n\t\t\t\t\t\t\t 3 for Accessories\n\t\t\t\t\t\t\t 4 for Services\n\t\t\t\t\t\t\t 5 for Contact Us");
        int no=input4.nextInt();
        if(no==1){
            System.out.println("\t\t\t\t\t\t\t*\t\t\t\t\t\t\t\t\t1.Laptop\t\t\t\t\t\t\t\t\t\t\t*");
            System.out.println(getLaptop());
        }
        else if(no==2){
            System.out.println("\t\t\t\t\t\t\t*\t\t\t\t\t\t\t\t\t2.Desktops\t\t\t\t\t\t\t\t\t\t\t*");
//
            System.out.println(getDesktop());
        }
        else if(no==3){
            System.out.println("\t\t\t\t\t\t\t*\t\t\t\t\t\t\t\t\t3.Accessories\t\t\t\t\t\t\t\t\t\t*");
           // addAccessoriesInventory();
        }
        else if(no==4){
            System.out.println("\t\t\t\t\t\t\t*\t\t\t\t\t\t\t\t\t5.Services    \t\t\t\t\t\t\t\t\t\t\t*");
        }
        else if(no==5){
            System.out.println("\t\t\t\t\t\t\t*\t\t\t\t\t\t\t\t\t6.Contact Us\t\t\t\t\t\t\t\t\t\t*");
        }
        else{
            System.out.println("You have entered incorrect option");
            specifyCategory();
        }
    }  

    public ArrayList<Laptop> getLaptop(){
        return (ArrayList<Laptop>) this.product;
    }

    public ArrayList<Desktop> getDesktop(){
        return (ArrayList<Desktop>) this.product;
    }

    public void setProduct(ArrayList<T> product) {
        this.product = product;
    }
}

2 个答案:

答案 0 :(得分:0)

我已在下面复制的部分代码中添加了一些注释,希望它有助于显示发生的情况。您的逻辑基本上是

  • 将5台笔记本电脑添加到列表中
  • 将5个桌面添加到另一个列表
  • 用笔记本电脑列表替换产品列表(执行5次)
  • 用台式机列表替换产品列表(执行5次)
  • 最终结果是产品列表包含桌面列表。

在Main中,您有

    // Create a laptop ArrayList and add 5 Laptops
    ArrayList<Laptop> laptop = new ArrayList<Laptop>();

    laptop.add(L1);
    laptop.add(L2);
    laptop.add(L3);
    laptop.add(L4);
    laptop.add(L5);
    laptop.add(L6);

    // Create a desktop ArrayList and add 5 Desktops
    ArrayList<Desktop> desktop = new ArrayList<Desktop>();
    desktop.add(D1);
    desktop.add(D2);
    desktop.add(D3);
    desktop.add(D4);
    desktop.add(D5);        

    // Loop 5 times
    for(int i = 0; i<laptop.size(); i++){
        // pass the laptop arraylist to P1.setProduct method
        // The setProduct method replaces the current product list with whatever you pass in
        P1.setProduct(laptop);
    }
    // loop 5 times
    for( int j=0;j<desktop.size();j++){
        // pass the desktop arraylist to P1.setProduct method
        // The setProduct method replaces the current product list with whatever you pass in
        P1.setProduct(desktop);
    }

在您拥有的产品中

public void setProduct(ArrayList<T> product) {
    this.product = product;
}

答案 1 :(得分:0)

@Zash这只是如何存储所有产品的一个示例。

要将所有台式机和笔记本电脑存储在一个产品列表中,您需要使台式机和笔记本电脑类共享一个子类或接口。通常最好是首选接口而不是子类,因此这是一个示例。

public interface Product {

    String getDescription();

}

台式机和笔记本电脑类

public class Desktop implements Product {

    private String description;

    public Desktop(String description) {
        this.description = description;
    }

    public String getDescription() {
        return description;
    }

}

public class Laptop implements Product {

    private String description;

    public Laptop(String description) {
        this.description = description;
    }

    public String getDescription() {
        return description;
    }
}

Products类用于存储所有台式机和笔记本电脑,并且还允许检索其中的列表

import java.util.ArrayList;
import java.util.List;

public class Products {

    List<Product> products = new ArrayList<>();

    public void addProduct(Product product) {
        products.add(product);
    }

    public List<Product> getProductsOfType(Class productType) {
        List<Product> productsOfType = new ArrayList<Product>();
        for (Product product : products) {
            if (productType.isInstance(product)) {
                productsOfType.add(product);
            }
        }
        return productsOfType;
    }

}

利用上述内容的主类

public class Main {

    public static void main(String[] args) {
        Products products = new Products();

        products.addProduct(new Laptop("Dell XPS i7|2.8GHZ|8th Gen"));
        products.addProduct(new Laptop("HP Probook i5|2.4GHZ|7th Gen"));

        products.addProduct(new Desktop("i5-2.8GHZ 6th Gen 1 TB"));
        products.addProduct(new Desktop("i7-2.6GHZ 7th Gen 2 TB"));

        System.out.println("Desktops : ");
        for (Product desktop : products.getProductsOfType(Desktop.class)) {
            System.out.println(desktop.getDescription());
        }

        System.out.println("Laptops : ");
        for (Product laptop : products.getProductsOfType(Laptop.class)) {
            System.out.println(laptop.getDescription());
        }

    }

}