|
i have an array named data, which contains n integers. i want to print all of the numbers, starting at the data[0]. but if the number 42 occurs, then i want to stop my printing just before the 42(without printing 42). here is most of my for-loop to accomplish my goal:
for (i = 0; i<=42; i++)
System.out.println(data);
what is the correct way to fill in the blank?
|
|
|
class PrintTest
{
public static void main(String[] args)
{
int[] data = {
22, 15, 39, 12, 16, 82, 42, 18, 44, 13
};
int stop = 42;
printArrayUpTo(data, stop);
stop = 44;
printArrayUpTo(data, stop);
stop = 99;
printArrayUpTo(data, stop);
}
private static void printArrayUpTo(int[] array, int n)
{
System.out.println("print up to: " + n);
for(int j = 0; j < array.length; j++)
{
if(array[j] == n)
break;
System.out.print(array[j]);
if(j < array.length-1 && array[j+1] != n)
System.out.print(", ");
}
System.out.println();
}
}
|
|
|
|
|
|
|
// |