Missing elements in array
I have a double array with 3 elements in it, however when I print the array it only shows the first two.
Here's my array:
Code :
private static String[][] commands = {{"commands", "new", "ls"},
{" - Lists current commands."," - Alows you to type a new password.", " - Lists currently written passwords."}}; // Array of string "commands".
Here's what it prints:
Code :
commands - Lists current commands.0
new - Alows you to type a new password.1
Here's my snippet of code:
Code :
if (cmd.equalsIgnoreCase("commands")) {
System.out.println("Current Commands:");
System.out.println(commands.length);
for (int i = 0; i < commands.length; i++)
System.out.println(commands[0][i] + commands[1][i] + i);
}
I don't see why it's doing this.
Re: Missing elements in array
Quote:
I have a double array
No you don't - that would be declared
You have an array of String arrays. You loop over the outer array - or that's what you would normally do if you loop from 0 to < commands.length - but you use the loop index i as the array index to the inner arrays. Your code should be something like
Code java:
for (int outer = 0; outer < outerArray.length; outer++)
for (int inner = 0; inner < outerArray[outer].length; inner++)
System.out.println("Array member at " + outer + ", " + inner + " is " + outerArray[outer][inner]);
Re: Missing elements in array
The output you posted suggests that you are going around the loop two times while I imagine that you want to go around the loop three times.
It is important to bear in mind that three are three arrays here. What is the value of commands.length which you use in the for loop? What about commands[0].length and commands[1].length? You can use System.out.println() to find out.
Code :
System.out.println("commands.length=" + commands.length);
System.out.println("commands.length[0]=" + commands.length[0]);
System.out.println("commands.length[1]=" + commands.length[1]);
if (cmd.equalsIgnoreCase("commands")) {
System.out.println("Current Commands:");
System.out.println(commands.length);
for (int i = 0; i < commands.length; i++)
System.out.println(commands[0][i] + commands[1][i] + i);
}
Re: Missing elements in array
Quote:
Originally Posted by
pbrockway2
The output you posted suggests that you are going around the loop
two times while I imagine that you want to go around the loop three times.
It is important to bear in mind that three are three arrays here. What is the value of commands.length which you use in the for loop? What about commands[0].length and commands[1].length? You can use System.out.println() to find out.
Code :
System.out.println("commands.length=" + commands.length);
System.out.println("commands.length[0]=" + commands.length[0]);
System.out.println("commands.length[1]=" + commands.length[1]);
if (cmd.equalsIgnoreCase("commands")) {
System.out.println("Current Commands:");
System.out.println(commands.length);
for (int i = 0; i < commands.length; i++)
System.out.println(commands[0][i] + commands[1][i] + i);
}
I wrote this quick little program to debug the array:
Code :
package encrypter;
public class test {
static String[][] commands = {{"commands", "new", "ls"},
{" - Lists current commands."," - Alows you to type a new password.", " - Lists currently written passwords."}};
public static void main(String[] args) {
System.out.println(commands[0].length);
System.out.println(commands[1].length);
}
}
the length of each array is 3, here's the outprint:
I'm coming to Java from Perl and in Perl a for loop wont stop until it's already read through everything. Is it different in Java?
Re: Missing elements in array
The for loop does what you'd expect - it increments until the condition part becomes false.
What you didn't do in your debugging code was print the actual array length you are using: commands.length
commands is one array with its own length. While commands[0] (and commands[1]) are different arrays with their own, very likely different, lengths. Print the value of all three and decide which you ought to use.
Re: Missing elements in array
commands has a length of 2. I'm assuming that the 2 represents the number of arrays? Quick question here, does that "2" include 0,1, and 2 or just 1 and 2?
Re: Missing elements in array
Yes, 2 is the number of elements that commands contains. They happen to be arrays. Specifically the elements are:
the array ["commands", "new", "ls"]
and the array [" - Lists current commands."," - Alows you to type a new password.", " - Lists currently written passwords."]
commands is an array with two elements. And both of those elements are arrays each with three elements.
I am not sure what you mean by asking what the 2 contains. commands.length is just 2: a number. You can think of it as the number of rows. And it's the wrong thing to use as your for loop condition because you don't mean to loop over the two rows, you mean to loop over each of the three elements of the rows.