gpt4 book ai didi

java - 如何从链表内的数组列表读取数据

转载 作者:行者123 更新时间:2023-12-02 09:46:30 24 4
gpt4 key购买 nike

我有一个包含年份和值的数组列表。该数组列表作为链接列表中的元素之一放置在链接列表中。

链表元素 -> 国家代码、indicatorName、indicatorCode 和数据(数组列表)

当我搜索指标代码然后搜索年份时,如何从数组列表中检索值?

应用程序类中我的代码段:

while(str2 != null ){
StringTokenizer st = new StringTokenizer(str2,";");
ArrayList <MyData> data = new ArrayList();

String cCode = st.nextToken();
String iName = st.nextToken();
String iCode = st.nextToken();

for (int j = 0; j < 59; j++){
String v = st.nextToken();
int year = 1960 + j;

d1 = new MyData (year,v);
data.add(d1);
}

Indicator idct1 = new Indicator (cCode,iName,iCode,data);
bigdata.insertAtBack(idct1);
str2 = br2.readLine();
}

//search
String search2 = JOptionPane.showInputDialog("Enter Indicator Code");

boolean found2 = false;
Indicator idct3 = (Indicator)bigdata.getFirst();

while (idct3 != null){

if ( idct3.getICode().equalsIgnoreCase(search2)){
int search3 = Integer.parseInt(JOptionPane.showInputDialog("Enter year"));
found2 = true;
searchYear(bigdata,search3);
}
if (!found2) {
System.out.println("Indicator Code is not in the data");
}
idct3 = (Indicator)bigdata.getNext();
}

public static void searchYear (myLinkedList bigdata, int searchTerm) {
Indicator idct4 = (Indicator)bigdata.getFirst();
boolean found = false;
while(idct4 != null){
if(idct4.getDYear() == searchTerm) {
found = true;
System.out.println(idct4.getDValue());
}
idct4 = (Indicator)bigdata.getNext();
}
if (!found){
String message = "Error!";
JOptionPane.showMessageDialog(new JFrame(), message, "Dialog",JOptionPane.ERROR_MESSAGE);
}
}

指标类别:

    public Indicator(String cCode, String iName, String iCode,ArrayList <MyData> DataList){
this.cCode = cCode;
this.iName = iName;
this.iCode = iCode;
this.DataList = DataList;
}

public String getCCode(){return cCode;}
public String getIName(){return iName;}
public String getICode(){return iCode;}
public int getDYear(){return d.getYear();}
public String getDValue(){return d.getValue();}

public void setCCode(String cCode){this.cCode = cCode;}
public void setIName(String iName){this.iName = iName;}
public void setICode(String iCode){this.iCode = iCode;}

public String toString(){
return (cCode + "\t" + iName + "\t" + iCode + "\t" + DataList.toString());
}
}

MyData 类:

    public MyData(int year, String value){
this.year = year;
this.value = value;
}

public int getYear(){return year;}
public String getValue(){return value;}

public void setYear(int year){this.year = year;}
public void setValue(String value){this.value = value;}

public String toString(){
return (value + "(" + year + ")");
}

最佳答案

我还无法添加评论,但这就是我的假设。

这里正在获取匹配的指标代码 (iCode),因此让我们将指标对象 (idct3) 传递到 searchYear 方法中,而不是重点关注其 ArrayList < MyData > DataList:

String search2 = JOptionPane.showInputDialog("Enter Indicator Code");

boolean found2 = false;
Indicator idct3 = (Indicator)bigdata.getFirst();

while (idct3 != null){

if ( idct3.getICode().equalsIgnoreCase(search2)){
int search3 = Integer.parseInt(JOptionPane.showInputDialog("Enter year"));
found2 = true;
searchYear(idct3,search3); // Indicator passed in here
}
if (!found2) {
System.out.println("Indicator Code is not in the data");
}
idct3 = (Indicator)bigdata.getNext();
}

让我们更改 searchYear 方法以获取 Indicator 类并搜索其 DataList:

public static void searchYear (Indicator indicator, int searchTerm) {
for (int i = 0; i < indicator.DataList.size(); i++) {
if (indicator.DataList.get(i).getYear() == searchTerm) { // Sequentially get MyData object from DataList and its year for comparison
System.out.println("Found year " + searchTerm + " with Indicator code " + indicator.getICode() + " having value " + indicator.DataList.get(i).getValue());
return; // Exit this method
}
}
System.out.println("Not Found");
}

关于java - 如何从链表内的数组列表读取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56611466/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com