0

I have 2 string arrays.

string [] first = {"ajunkbc","ajunkHello","adedbc","abcjunk","add","ad","a","","junk","ajunk","aajunkbb"};
String  [] second = {"abc","aHello","adedbc","abcjunk","add","ad","a","","junk","a","aajunkbb"};

I'd like the result of my merge() method to concatenate each element from the first array with the respective element of the second array separated by a comma.

Below is my code

private static String[] merge(String [] tests, String [] expectations){

    List<String> testList = Arrays.asList(tests);
    List<String> expectationsList = Arrays.asList(expectations);

    List<String> retList = new ArrayList<String>();
    for(String test : testList){
        for(String val : expectationsList){
            retList.add(test+","+val);
            break;
        }
    }

This does not work. What's wrong with my code?

6
  • Why do you think it should work? What do you think for(String val : expectationsList) does?
    – Pshemo
    Commented Aug 27, 2015 at 20:09
  • it loops over each element of the expectationsList. Am I approaching this problem wrong? Commented Aug 27, 2015 at 20:15
  • How exactly iteration works? What happens when you invoke for(String val : expectationsList)?
    – Pshemo
    Commented Aug 27, 2015 at 20:19
  • Hmm, I guess it just moves from one element to the next. ? Commented Aug 27, 2015 at 20:28
  • That is what happens in for(...){here}. I want to ask you what happens for (here){...}
    – Pshemo
    Commented Aug 27, 2015 at 20:33

2 Answers 2

4

What's wrong is that you are looping over expectationsList and breaking out of the loop after the first iteration:

for(String val : expectationsList){
    retList.add(test+","+val);
    break; //<--- breaking out of loop after first iteration each time
}

So the result is that you are always retrieving the first element of expectationsList.

Since what you want is to loop over two arrays, you should use an index:

for (int i = 0; i < testList.size(); i++) {
    retList.add(testList.get(i)+","+expectationsList.get(i));
}

Also, note that this implies that the size of testList is the same as the size of expectationsList. Your method should probably throw an exception if this is not the case.

Note that you do not need to convert the input arrays into lists. You can use them as-is.

private static String[] merge(String[] tests, String[] expectations) {
    if (tests.length != expectations.length) {
        throw new IllegalArgumentException("input not of same length");
    }
    String[] result = new String[tests.length];
    for (int i = 0; i < tests.length; i++) {
        result[i] = tests[i] + "," + expectations[i]);
    }
    return result;
}

Java 8 solution:

private static String[] merge(String[] tests, String[] expectations) {
    if (tests.length != expectations.length) {
        throw new IllegalArgumentException("input not of same length");
    }
    return IntStream.range(0, tests.length).mapToObj(i -> tests[i] + "," + expectations[i]).toArray(String[]::new);
}
1

You're iterating through each member of testList and then for each one, iterating through each member of expectationsList. You want to iterate through each of both of them together.

What you want to do is something like this:

private static String[] merge(String[] tests, String[] expectations) {
    String[] result = new String[tests.length];
    for(int i = 0; i < tests.length; i++) {
        result[i] = tests[i] + "," + expectations[i];
    }
    return result;
}

This code makes the assumption that tests and expectations have the same length. You might want to do a check for that at the beginning:

if (tests.length != expectations.length) {
    throw new IllegalArgumentException("tests and expectations are of different lengths")
}

Notice how now you're getting the element at the same index from both arrays.


Sidenote: You can iterate over arrays with the for each format. This works just fine:

String[] myStringArray = getStringArray();
for (String myString : myStringArray) {
    // Do something
}

You don't need to convert to a List in order to iterate :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.