как читать данные из архива, который находится внутри списка ссылок - PullRequest
0 голосов
/ 15 июня 2019

У меня есть архивист года и значения. этот массив размещается в связанном списке как один из элементов в связанном списке.

элементы связанного списка -> код страны, имя индикатора, код индикатора и данные (массив данных)

как я могу получить значение из массива при поиске кода индикатора, а затем года?

сегментов моего кода из класса приложения:

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 Ответ

1 голос
/ 15 июня 2019

Я пока не могу добавить комментарий, но вот что я предполагаю.

Здесь он получает соответствующий код индикатора (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, чтобы взять класс 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");
}
...