【发布时间】:2019-06-17 13:51:31
【问题描述】:
我已经构建了一个 BST,其中包含(国家代码(字符串)、指标代码(字符串)、指标名称(字符串)和年份数组列表(整数)和值(字符串))。
我试图弄清楚如何通过输入指标代码和输出值的年份来提示用户进行搜索。
如果您能说明如何对搜索方法进行编码,将不胜感激,因为我已经尝试了所有方法。
我在 BST 课上试过这个。但感觉不对(?)
public void search(Indicator indicator, String searchTerm){
String str = (String)indicator.getICode();
int n1 = str.compareTo(searchTerm);
int n2 = searchTerm.compareTo(str);
if (str == null || str.equalsIgnoreCase(searchTerm)){
return str;
}
if (n1 > n2){
return search(indicator, searchTerm);
}
else if (n1 < n2){
return search(indicator, searchTerm);
}
}
这是我的应用程序类:
public class BigDataBST{
public static void main (String [] Args) throws IOException {
try{
BST bigdata = new BST();
MyData d1;
File inFile = new File ("Indicator.txt");
FileReader fr = new FileReader (inFile);
BufferedReader br = new BufferedReader(fr);
String str = br.readLine();
while(str != null ){
StringTokenizer st = new StringTokenizer(str,";");
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 idct = new Indicator (cCode,iName,iCode,data);
bigdata.insertNode(idct);
str = br.readLine();
}
TreeNode 类:
TreeNode left;
TreeNode right;
Indicator idct;
public TreeNode(Indicator id){
left = right = null;
idct = id;
}
指标类:
private String cCode; //country code
private String iName; //indicator Name;
private String iCode; //indicator code;
public ArrayList <MyData> DataList;
public Indicator(){
cCode = null;
iName = null;
iCode = null;
DataList = null;
}
public Indicator(String cCode, String iName, String iCode,ArrayList <MyData> DataList){
this.cCode = cCode;
this.iName = iName;
this.iCode = iCode;
this.DataList = DataList;
}
//setter & getter method for attributes iCode,iName and cCode
//toString method
MyData 类:
private int year;
private String value;
public MyData(){
year = 0;
value = null;
}
public MyData(int year, String value){
this.year = year;
this.value = value;
}
//setter & getter method for attributes year and value
//toString method
indicator.txt 示例:
(左起:cCode;iName;iCode;值)
我的;工业就业(占总就业的百分比)(模拟国际劳工组织估计); SL.IND.EMPL.ZS;
29,08600044;28,56900024;28,36300087;28,02300072;27,51600075;27,48699951;27,39800072;27,30500031
【问题讨论】:
-
提供更多代码,即 indicator.java、BigData.java 并请对这些 icode、ccode 进行一些注释,所有这些都有些混乱...还提供输入文件的示例...在您的
search()你为什么要使用Indicator使用TreeNode并称之为递归。 -
@ZainArshad 我已经更新了你想看的东西。至于搜索方法,我不太确定我在做什么,因为我所做的每一项研究都是基于整数的。