按属性搜索项目阵列列表



我有一个具有3个属性,优先级,描述和参考#的对象/项目的数组列表。该程序应允许您根据项目的参考#从ArrayList打印项目。由于某种原因,编译器不允许我通过阵列列表迭代以找到匹配的项目。我坚持使用的部分(在方法" FindbyRefer"中):

for(Object list : list.getMyList()){
                if(list.getReference.equals(num)){
                    System.out.println(list);
                }     

我的主要:

public class Main {
public static void main(String[] args) {
    // TODO Auto-generated method stub
    Scanner myscanner = new Scanner(System.in);
    boolean exit = false;
    boolean error = false;
    boolean error1 = false;
    String input = "";
    int num = 0;
    System.out.println("Welcome to Your To-Do List!n1 = Create New Item n2 = Exit n3 = Display Item n4 = Delete Item");
    while(exit == false){
    Item item = new Item();
    do{
        try {
            System.out.println("Enter a command ");
            input = myscanner.nextLine();
            num = Integer.parseInt(input);
            if(num ==  1){
                item.reference();
                System.out.println("Reference: "+ item.getReference() + "nDescription: " + item.getDescription() + "nPriority: " + item.getPriority());
                error = true;
            }
            /**
             * This creates the item
             */
            else if(num == 2){
                exit = true;
                System.out.println("Bye"); break;
            }
            else if(num == 3){
                item.findbyrefer();
                /**
                 * This is the part I'm stuck at
                 */
            }
            else{
                error = true;
                System.out.println("invalid");
            }
        }
        catch(Exception e){
            System.out.println("invalid input");
            error = true;
        }
    }
    while(error);   
    }
}

我的项目类:

 public class Item {
private Scanner myScanner;
private int reference;
private String description;
private int priority;
List list = new List();
public void setReference(int reference) {
    this.reference = reference;
}
public int getReference() {
    return reference;
}
public void setDescription(String description) {
    this.description = description;
}
public String getDescription() {
}
public void setPriority(int priority) {
    this.priority = priority;
}
public int getPriority() {
    return priority;
}
public void reference(){
    boolean error = false;
    int x = 0;
    do{
        try{
            System.out.println("Assign this item with a reference number: ");
            myScanner = new Scanner(System.in);
            x=myScanner.nextInt();
            setReference(x);
            error=false;
            break;
        }
        catch(Exception e){
            error = true;
            System.out.println("invalid");
        }
    } while(error);
    description();
}
public void description(){
    boolean error = true;
    while(error){
        try{
            System.out.println("Enter the description: ");
            myScanner = new Scanner(System.in);
            String input = myScanner.nextLine();
            setDescription(input);
            error=false;
            break;
        }
        catch(Exception e){
            error = true;
            System.out.println("invalid");
            break;
        }
    }
    priority();
}
public void priority(){
    boolean error = false;
    do{
        try{
            System.out.println("Assign this item with a priority number: ");
            myScanner = new Scanner(System.in);
            setPriority(myScanner.nextInt());
            error=false;
        }
        catch(Exception e){
            error = true;
            System.out.println("invalid");
        }
    }
    while(error==true);
    list.addItem(this);
    System.out.println(list.getMyList());
}
public void findbyrefer(){
    boolean error1 = false;
    String input = "";
    int num = 0;
    do{
        try{
            System.out.println("Enter the reference number of the item you want to show");
            myScanner = new Scanner(System.in);
            input = myScanner.nextLine();
            num = Integer.parseInt(input);
            for(Object list : list.getMyList()){
                if(list.equals(num)){
                    System.out.println(list);
                }
                else{
                    System.out.println("not working");
                }
            }
        }
        catch(Exception e){
            error1 = true;
            System.out.println("invalid");
        }
    }
    while(error1 = true);
}

}我的列表类包含我的实际arraylist:

public class List {
public ArrayList<Object> MyList = new ArrayList<Object>();
public void setMyList(ArrayList<Object> myList) {
    this.MyList = myList;
}
public ArrayList<Object> getMyList() {
    return MyList;
}
public void addItem (Object t){
    getMyList().add(t);
    }

Object上没有getReference方法。

由于您的arraylist包含 Item s,请告诉它:

ArrayList<Item> myList = new ArrayList<>();
// ------^^^^^^

现在查看您的循环:

for(Object list : list.getMyList()){
    if(list.getReference.equals(num)){
        System.out.println(list);
    }
}

我们需要:

  1. Object更改为Item
  2. 使用与list不同的标识符进行项目(只是为了避免混乱)
  3. 呼叫 getReference(添加()
  4. 使用==,而不是equals进行检查numequals用于对象)

so:

for (Item item : list.getMyList()) {
    if (item.getReference() == num){
        System.out.println(item);
    }
}

add()括号列表列表和使用方法(==)等于运算符。

 for(Object list : list.getMyList()){
         if(list.getReference() == num)){
            System.out.println(list);
         }
}

最新更新