简体   繁体   English

比较两组字符串

[英]Comparing two set of strings

Here is my problem. 这是我的问题。 I'm trying to compare two different string by using && and also .equals but seems like it cannot give me the result it should be. 我正在尝试使用&&.equals来比较两个不同的字符串,但似乎无法给我应该的结果。

Here is my code (starting from where I think is the problem is) : 这是我的代码(从我认为是问题的地方开始):

    for(int i = 0; i < datalist.size(); i+=3)   {       

        String temp1 = datalist.get(i);
        String temp2 = datalist.get(i+1);

        System.out.println(temp1);
        System.out.println(temp2);

        if (temp1.equals(dataquery1)) {

            System.out.println("TRUE");

            if (temp2.equals(dataquery2))   {

            System.out.println("TRUE");
            array2.add((datalist.get(i)));
            array2.add((datalist.get(i+1)));
            array2.add((datalist.get(i+2)));

            }   
        }

    }

    System.out.println("\n\nArray2 size : " + array2.size());
    for (int j = 0; j < array2.size(); j++) {

        System.out.println("Array2 : " + array2.get(j));

    }

This is the array : 这是数组:

[0]  Lipase B
[1]  X-33
[2]  pPICZ?A
[3]  Candida antarctica lipase B (CALB)
[4]  SMD1168H
[5]  pGAP?B
[6]  Lip 2
[7]  X-33
[8]  pPICZ?A

And the result is : 结果是:

Lipase B
X-33
Candida antarctica lipase B (CALB)
SMD1168H
Lip 2
X-33


Array2 size : 0

The result it should be is : 结果应该是:

TRUE
TRUE

Array2 size : 3
Array2 : Lipase B
Array2 : X-33
Array2 : pPICZ?A

I tried using if (temp1.equals(dataquery1) && temp2.equals(dataquery2)) but it doesn't work. 我尝试使用if (temp1.equals(dataquery1) && temp2.equals(dataquery2))但是它不起作用。 However if I change the dataquery1 and dataquery2 with its value Lipase B and X-33 respectively, the code works fine. 但是,如果我dataquery2用值Lipase BX-33更改dataquery1dataquery2 ,则代码可以正常工作。

Can anyone help? 有人可以帮忙吗?

By the looks of things, you're setting dataquery1 to "Lipase" when you should be setting it to "Lipase B". 从外观上看,您应该将dataquery1设置为“ Lipase”,而应将其设置为“ Lipase B”。 If you fix that, I get the correct output: 如果解决此问题,我将获得正确的输出:

Lipase B
X-33
TRUE
TRUE
Candida antarctica lipase B (CALB)
SMD1168H
Lip 2
X-33


Array2 size : 3
Array2 : Lipase B
Array2 : X-33
Array2 : pPICZ?A

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM