in your expression you iterate through the values of the array, so for
for (int i: inputArray)
values of i will be: 4, 6, 5, -10, 8, 1, 2 sequentially.
Judging by the style of your code, you need
for (int i = 0; i < inputArray.length; i++)
or similar.
Same story with streams. If you create a primitive stream out of an array of integers, foreach would iterate over its values, not indexes. So, you would need to account for indexes yourself. Here is a possible implementation.
private static int i = 0, j= 0;
IntStream iStream = Arrays.stream(inputArray);
iStream.forEachOrdered(ival -> {
j = i+1;
if (j < inputArray.length) {
IntStream jStream = Arrays.stream(inputArray, j, inputArray.length);
jStream.forEachOrdered(jval -> {
System.out.println(i + "+" + j + ": " + (ival + jval));
j++;
});
}
i++;
});
I believe that the above would explain the main idea with streams.
you can also use IntStram.range() to iterate across indexes themselves, or use other possible streaming solutions. Please see other answers in this thread as well.
inputArray[-9]isn't going to work (wheniis-10for example).if(inputArray[i]+inputArray[j] == inputNumber)