按属性搜索项目ArrayList

时间:2017-03-10 08:20:44

标签: java oop arraylist

我有一个包含3个属性的对象/项的ArrayList,Priority#,Description和Reference#。该程序假设允许您根据项目的参考号#打印Arraylist中的项目。由于某种原因,编译器不允许我遍历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);
}

} My List Class包含我的实际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);
    }

2 个答案:

答案 0 :(得分:0)

为列表的getReference方法添加()括号并使用(==)等于运算符。

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

答案 1 :(得分:0)

$(document).ready(function(){ $("#step3-drpdwn").on("change", function(){ $(this).css("color", "#000"); }); }); 上没有getReference方法。

由于您的ArrayList包含Object,因此请告知:

Item

现在看看你的循环:

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

我们需要:

  1. for(Object list : list.getMyList()){ if(list.getReference.equals(num)){ System.out.println(list); } } 更改为Object
  2. 为项目使用与Item不同的标识符(仅为避免混淆)
  3. 致电 list(添加getReference
  4. 使用()而非==来检查equalsnum是否为对象)
  5. 所以:

    equals