1

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.nextToken();
        }
    }
}

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

Gray
  • 115,027
  • 24
  • 293
  • 354
Roubie
  • 299
  • 1
  • 6
  • 16
  • I don't understand your question. What should be result of this comparison? Are you testing if these arrays contains same values or just counting number of similar? – Gaim Aug 10 '11 at 05:57
  • By the way you should programme to the interface so it would be better to use `List input = new ArrayList();` – Gaim Aug 10 '11 at 06:00
  • Please go through this link.. http://stackoverflow.com/questions/919387/how-can-i-calculate-the-difference-between-two-arraylists – Master Stroke Aug 10 '11 at 07:17
  • Please Go Through this link.. http://stackoverflow.com/questions/919387/how-can-i-calculate-the-difference-between-two-arraylists – Master Stroke Aug 10 '11 at 07:18
  • Can you edit your question and add quotes (") where necessary. I assume input = {"a b c d", "a g f r"... is right? – Gray Sep 01 '11 at 19:25

2 Answers2

0

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...
             }
         }
     }
JKo
  • 1,260
  • 1
  • 10
  • 18
0

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.

Gaim
  • 6,734
  • 4
  • 38
  • 58