开发者

how to compare value in ArrayList<String> in java

i have two ArrayList which are:

ArrayList<String> input = new ArrayList<String>();
ArrayList<String> output = new ArrayList<String>();

input contains these data:
input= {a b c d, a g f r, d e a b, k c s x, f g h s}
output contains:
output = {a b c f, g f x r, d e f g}

i would like to compare the value of those list such as: input(0) contains a,b,c and d while output(0) contains a,b,c and f.if we compare we can get a,b and c are the similar value means both arrays have 3 similar values.

Iteration<String> itr = input.iterator();

while(itr.hasNext()) {

    //should i do this to get each value in each row?
    for(String s : input) {
        StringTokenizer b = new StringTokenizer(s," ");
        String[] temp_input = new String[n];
        int i = 0;

        while(b.hasMoreTokens()) {
            temp_input[i] = b.nextToke开发者_运维问答n();
        }
    }
}

so the result should be the number of similar values btween input and output. thanks in advance..


May I suppose that arrays are sorted and you are interested only in comparison input[i] to output[i]?

If so, there is simple solution:

int counter = 0;
int length = Math.min(input.size(), output.size());
String[] data = null;
for (int i = 0; i < length; ++i ) {
    data = input.get(i).split(" "); // division by space
    for(String value : data) {
        if ( output.get(0).contains(value) ) ++counter;
    }    
}

I should mention that this solution is not in the best performance but according to your poor problem specification it could be suitable.


Here's an idea:

     for(String string1: arrayOfStrings1)
     {
         for(String string2: arrayOfStrings2)
         {
             if(string1.equals(string2))
             {
                  //increase counter or do whatever you would like here...
             }
         }
     }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜