Java 按属性搜索项ArrayList
我有一个对象/项的数组列表,它有3个属性:优先级、描述和引用。假定程序允许您根据项目的引用#从Arraylist打印项目。出于某种原因,编译器不允许我遍历ArrayList来查找匹配项。 我被卡住的部分(在“findbyrefer”方法中): 我的主要意见是:Java 按属性搜索项ArrayList,java,oop,arraylist,Java,Oop,Arraylist,我有一个对象/项的数组列表,它有3个属性:优先级、描述和引用。假定程序允许您根据项目的引用#从Arraylist打印项目。出于某种原因,编译器不允许我遍历ArrayList来查找匹配项。 我被卡住的部分(在“findbyrefer”方法中): 我的主要意见是: public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner myscann
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的My List类:
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);
}
公共类列表{
public ArrayList MyList=new ArrayList();
公共无效集合列表(ArrayList myList){
this.MyList=MyList;
}
公共阵列列表getMyList(){
返回MyList;
}
公共无效附加项(对象t){
getMyList().add(t);
}
为列表的getReference方法添加()括号,并使用(=)相等运算符
for(Object list : list.getMyList()){
if(list.getReference() == num)){
System.out.println(list);
}
}
为list的getReference方法添加()括号,并使用(=)相等运算符
for(Object list : list.getMyList()){
if(list.getReference() == num)){
System.out.println(list);
}
}
对象上没有getReference
方法
由于您的ArrayList包含项
s,请让它知道:
ArrayList<Item> myList = new ArrayList<>();
// ------^^^^^^
我们需要:
将对象
更改为项目
为项目使用不同于列表
的标识符(以避免混淆)
调用getReference
(添加()
)
使用=
,而不是equals
,检查num
(equals
用于对象)
因此:
对象上没有getReference
方法
由于您的ArrayList包含项
s,请让它知道:
ArrayList<Item> myList = new ArrayList<>();
// ------^^^^^^
我们需要:
将对象
更改为项目
为项目使用不同于列表
的标识符(以避免混淆)
调用getReference
(添加()
)
使用=
,而不是equals
,检查num
(equals
用于对象)
因此:
这是一个问题。这是一个问题。这是为了让你开始。肯定还有其他后续问题,彻底的代码审核是不合适的。但是如果你遇到进一步的问题,仔细阅读错误消息,做研究,检查Java书籍和教程,等等。如果你真的陷入困境,发布一个关于这个问题的新问题特定的事情,你被困在一个(注意“最小”)演示特定的问题。这是为了让你开始。肯定会有其他后续问题,彻底的代码审核对你来说是离题的。但是如果你遇到进一步的问题,仔细阅读错误消息,做研究,检查Java书籍和教程等。如果你真的陷入困境,发布一个关于特定问题的新问题你被困在一个(注意“最小”)演示特定问题的例子中。
for (Item item : list.getMyList()) {
if (item.getReference() == num){
System.out.println(item);
}
}