I wanted to try to set one argument args[0] , depending on which array will be displayed in direct or reverse order.
On the line, if(args[0].compareTo("First") == 0) throws ArrayIndexOutOfBoundsException: 0 .
What is the problem?
public class Main { public static void main(String[] args) { int[] massive = { 1, 2, 3, 4, 5 }; if (args[0].compareTo("First") == 0) { for (int i = 0; i < massive.length; i++) { System.out.println(massive[i]); } } else if (args[0].compareTo("Second") == 0) { for (int i = massive.length; i > 0; i--) { System.out.println(massive[i]); } } else if (args[0] == null || args.length == 0) { System.out.println("Third"); } } }
int i = massive.length;- array indices from0tolength - 1- zRrrargs.length == 0 || args[0] == nullargs.length == 0 || args[0] == null, and not vice versa, because in your version the second condition is meaningless. - Regent