coco
coco

Reputation: 95

compare list and array

I need to compare the value from List with the value from array. I wrote the following:

public class JavaApplication3 {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // TODO code application logic hereut
    List<String> l = new ArrayList<String>();
    l.add("test");
    l.add("b");
    String v = "";
    String s = "";
    String[] arr = {"test", "c", "b"};

    for (int i = 0; i < l.size(); i++){
        v = "";
        s = "";
        //System.out.println(l.get(i));
        for (int j = 0; j < arr.length; j++){
            if (l.get(i).equals(arr[j])){
                s = i + "";
            }else{
                s = arr[i];
            }
            v = v + s + ",";                   
        }  
        System.out.println(v);          
    }

}

}

I obtain the following 0,test,test, c,c,1 but I need the result like this: 0, c, 1,

Upvotes: 0

Views: 5805

Answers (5)

Sello
Sello

Reputation: 297

Well I suggest this:

     List<String> l = new ArrayList<String>();
     l.add("test");
     l.add("b");
     String[] arr = {"test", "c", "b"};

     for(int i=0;i<arr.length;++i){

       if(l.contains(arr[i]))
         s = ""+l.indexOf(arr[i]);
       else
         s = arr[i];

       v = v + s + ",";
      }

If got what you saying correct,I think this is less verbose

Upvotes: 0

GuessBurger
GuessBurger

Reputation: 478

Try to break things down to their high level steps.

For each string in the array
    find its place in the list
    if the item is in the list
        print its position
    else
        print the missing string
    print a common and space

Once you have this you can spot that find its place in the list could be a method that returns the place in the list or -1 if it isn't in the list. Here's what I made (might have renamed a few things and used a StringBuilder but you can ignore that for the moment).

import java.util.ArrayList;
import java.util.List;

public class Example {
    public static void main(final String[] args) {
        final List<String> listToSeach = new ArrayList<String>();
        listToSeach.add("test");
        listToSeach.add("b");

        final String[] arrayElementsToFind = { "test", "c", "b" };

        final StringBuilder output = new StringBuilder();
        for (final String string : arrayElementsToFind) {
            final int firstIndex = findFirstIndex(listToSeach, string);
            if (firstIndex > -1) {
                output.append(firstIndex);
            } else {
                output.append(string);
            }
            output.append(", ");
        }
        System.out.println(output);
    }

    private static int findFirstIndex(final List<String> list,
            final String element) {
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).equals(element)) {
                return i;
            }
        }
        return -1;
    }
}

Upvotes: 0

Elbek
Elbek

Reputation: 3484

How about this:

 public static void main(String[] args) {
        // TODO code application logic hereut
        List<String> l = new ArrayList<String>();
        l.add("test");
        l.add("b");
        String v = "";
        String s = "";
        String[] arr = {"test", "c", "b"};
        int pointer = 0;
        for (int i = 0; i < l.size(); i++){
                        //System.out.println(l.get(i));
            for (; pointer < arr.length;){
                if (l.get(i).equals(arr[pointer])){
                    s = i + "";
                    v = v + s + ",";
                    pointer++;
                    break;
                }else{
                    s = arr[i];
                }
                pointer++;
                v = v + s + ",";
            }

        }
        System.out.println(v);

    }

Upvotes: 0

Andy Thomas
Andy Thomas

Reputation: 86381

You have six iterations, each of which inserts something into the output.

You want three iterations, each of which checks for membership in the first list. You can do that with the List.contains() method. (If the list were long, you might want to consider using a Set instead of a List, to allow checking set membership more quickly.)

Upvotes: 0

Andreas Dolk
Andreas Dolk

Reputation: 114757

Looking at your expected result I guess the requirement like that:

for each element in the array, check if it is on the list. If it is on the list, print the index from the list for this element, otherwise print the element itself. So the algorithm should do:

array[0] = "test" -> found at index 0 -> print "0"
array[1] = "c"    -> not found        -> print "c"
array[2] = "b"    -> found at index 1 -> print "1"

The outer loop should iterate over the array. Then, for each array item, iterate over the list until you find the same element. For a first draft, don't collect the output in a string but print it immediatly. You can create the string when the algorithm works as expected.

Upvotes: 1

Related Questions