如何从链表内部的arraylist中读取数据

时间:2019-06-15 14:56:02

标签: java arraylist linked-list

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

链表元素->国家代码,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 + ")");
    }

1 个答案:

答案 0 :(得分:1)

我还不能添加评论,但这是我的假设。

这里获得了匹配的指标代码(iCode),因此我们将指标对象(idct3)传递到searchYear方法中,而不是专注于其ArrayList 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方法以使用指标类并搜索其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");
}