I'm studying CS and am still a Java / programming noob, we've got following code in class and have to explain the problems with the following method and are supposed to optimize it:
public static void printData(int[] list) {
int i = 0;
do {
System.out.println(list[i++]);
} while (i < list.length);
}
So, my first thought is that it's of course possible to use a do-while loop, but that I would have used a for loop such as:
public static void pd(int[] list) {
for (int i = 0; i < list.length; i++)
System.out.println(list[i]);
}
for optimization.
Now, the other method is also possible tho and I cannot figure out the problem with it, maybe that it's not too compatible because of the static
declaration, but I'm starting to wrap my head around how to deal with calling non-static
methods from main, if you have any other advice, I've got open ears.