Java 如何在嵌套的ArrayList中搜索一串数字?
我需要创建一个方案,其中一个选项应该是项目的最后期限。我有一个名为NewProject的类,然后在ProjectOptions文件中有一个选项来更改到期日期。这就是问题所在。我需要输入一个项目编号(projNum),然后在数组列表中搜索它,然后允许用户更改截止日期。我该怎么做呢 该类编程如下所示:Java 如何在嵌套的ArrayList中搜索一串数字?,java,string,arraylist,Java,String,Arraylist,我需要创建一个方案,其中一个选项应该是项目的最后期限。我有一个名为NewProject的类,然后在ProjectOptions文件中有一个选项来更改到期日期。这就是问题所在。我需要输入一个项目编号(projNum),然后在数组列表中搜索它,然后允许用户更改截止日期。我该怎么做呢 该类编程如下所示: package poisedProjects; public class NewProject { //Atributes String projNum; Str
package poisedProjects;
public class NewProject {
//Atributes
String projNum;
String projName;
String projType;
String projAddress;
String erfNum;
String projCost;
String projPaid;
String projDeadLine;
// Methods
public NewProject(String projNum, String projName, String projType, String projAddress, String erfNum, String projCost, String projPaid, String projDeadLine) {
this.projNum = projNum;
this.projName = projName;
this.projType = projType;
this.projAddress = projAddress;
this.erfNum = erfNum;
this.projCost = projCost;
this.projPaid = projPaid;
this.projDeadLine = projDeadLine;
}
//Methods(toString)
public String toString() {
String output = "Project Number: " + projNum;
output += "\nProject Name: " + projName;
output += "\nProject Type: " + projType;
output += "\nProject Address: " + projAddress;
output += "\nERF Number: " + erfNum;
output += "\nProject Total Cost: " + projCost;
output += "\nProject Cost Paid: " + projPaid;
output += "\nProject Deadline: " + projDeadLine;
return output;
}
public boolean contains(String search) {
return this.projNum.equals(search) || this.projName.equals(search) || this.projType.equals(search) || this.projAddress.equals(search) || this.erfNum.equals(search) || this.projCost.equals(search) || this.projPaid.equals(search) || this.projDeadLine.equals(search);
}
}
ProjectOptions文件包含以下内容:
...
import java.util.ArrayList;
import java.util.Scanner;
public class ProjectOptions {
public static void main(String[] args){
final Scanner scan = new Scanner(System.in);
ArrayList<NewProject> newProjects = new ArrayList<NewProject>();
...
else if(answer == 2) {
System.out.println("Enter the Project Number: \n");
String projNum = scan.toString();
boolean projfound = false;
while(projfound == false) {
boolean currentProj = newProjects.contains(projNum);
String currentProj2 = String.valueOf(currentProj);
if(currentProj2 == projNum) {
System.out.println("Enter new Due Date: \n");
String projDeadLine = scan.toString();
System.out.println("The new Due Date is: \t" + projDeadLine );
}
}
}
。。。
导入java.util.ArrayList;
导入java.util.Scanner;
公共类项目选项{
公共静态void main(字符串[]args){
最终扫描仪扫描=新扫描仪(System.in);
ArrayList newProjects=新ArrayList();
...
如果(答案==2){
System.out.println(“输入项目编号:\n”);
字符串projNum=scan.toString();
布尔projfound=false;
while(projfind==false){
布尔currentProj=newProjects.contains(projNum);
String currentProj2=String.valueOf(currentProj);
if(currentProj2==projNum){
System.out.println(“输入新的到期日:\n”);
String projDeadLine=scan.toString();
System.out.println(“新的到期日为:\t”+projheadline);
}
}
}
提前感谢您的帮助,我非常感谢。因此我认为用于字符串比较的.equals()方法应该可以工作
简单的例子:
List<String> stringList = new ArrayList<>();
stringList.add("20");
stringList.add("33");
stringList.add("50");
public static void findStringInStringList(String str) {
for (int i = 0; i < stringList.size(); i++) {
if (stringList.get(i).equals(str)) {
// do something
System.out.println("Match");
} else {
System.out.println("No Match");
}
}
}
List-stringList=new-ArrayList();
添加(“20”);
stringList.添加(“33”);
添加(“50”);
公共静态void findStringInStringList(字符串str){
对于(int i=0;i
您目前面临的问题是什么?仅供参考,对于字符串比较,您需要使用.equals()。projNum
是布尔值,因此以下条件总是错误的:```如果(currentProj2==projNum)...``我认为您根本不需要任何条件,只要删除它,它就可以正常工作。