根据用户输入在 arraylist<String[]> 中搜索

Searching in arraylist<String[]> from user input

我想根据用户输入在数组列表中进行搜索,但我的 if 条件似乎不起作用。使用 boolean 和 .contains() 也不适用于我的程序。这是编码:

String phone;
phone=this.text1.getText();
System.out.println("this is the phone: " + phone);

BufferedReader line = new BufferedReader(new FileReader(new File("C:\Users\Laura Sutardja\Documents\IB DP\Computer Science HL\cs\data.txt")));
    String indata;

    ArrayList<String[]> dataArr = new ArrayList<String[]>(); 

    while ((indata = line.readLine()) != null) { 

        String[] club = new String[2]; 
        String[] value = indata.split(",", 2);  
        //for (int i = 0; i < 2; i++) { 

            int n = Math.min(value.length, club.length);
              for (int i = 0; i < n; i++) { 
               club[i] = value[i]; 
        }
          boolean aa = dataArr.contains(this.text1.getText());

          if(aa==true)
          text2.setText("The data is found.");
          else
          text2.setText("The data is not found.");

        dataArr.add(club);
    }


    for (int i = 0; i < dataArr.size(); i++) {
        for (int x = 0; x < dataArr.get(i).length; x++) {
            System.out.printf("dataArr[%d][%d]: ", i, x);
            System.out.println(dataArr.get(i)[x]);
        }
    }


 }
 catch ( IOException iox )
{
  System.out.println("Error");
}

您的 dataArrString[] 的列表,而您正在搜索 String。两者是不同种类的对象。

我真的不知道 club 数组的内容是什么样的,但是你应该更改 dataArr 以保持普通 String,或者写一个在 dataArr 中迭代查找包含 this.text1.getText().

输出的 String[] 的方法

程序有很多错误。我假设您想读取一个文本文件并将每一行存储在数组列表中。为此,您必须拆分文本文件的每一行并将该数组存储在 arrayList 中。

String[] value;
while ((indata = line.readLine()) != null) { 
    value = indata.split(",");
    dataArr.add(value);
}

现在您在 arrayList 中有了文件的内容。 接下来您要将用户输入与数组列表的每个元素进行比较。

int j = 0;
for (int i = 0; i < dataArr.size(); i++) {
    String[] phoneData = dataArr.get(i);
    if (phoneData[1].equals(phone)) { // i am assuming here that the phone number is the 2nd element of the String[] array, since i dont know how the textfile looks.
        System.out.println("Found number.");
        club[j++] = phoneData[1];
    } else if (i == dataArr.size()-1) {
        System.out.println("Didn't find number.");
    }
}

编辑: 根据要求:

    String phone;
    phone = "38495";
    System.out.println("this is the phone: " + phone);

    BufferedReader line = new BufferedReader(new FileReader(new File("list.txt")));
    String indata;

    ArrayList<String[]> dataArr = new ArrayList<>();
    String[] club = new String[2];
    String[] value;// = indata.split(",", 2);
    while ((indata = line.readLine()) != null) {
        value = indata.split(",");
        dataArr.add(value);
    }
    int j = 0;
    for (int i = 0; i < dataArr.size(); i++) {
        String[] phoneData = dataArr.get(i);
        if (phoneData[1].equals(phone)) {
            System.out.println("Found number.");
            club[j++] = phoneData[1];
            break;
        } else if (i == dataArr.size()-1) {
            System.out.println("Didn't find number.");
        }
    }

我希望这现在有意义。