Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Missing elements in array

  1. #1
    Member
    Join Date
    Jan 2011
    Posts
    88
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Exclamation 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:
    	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:
    commands - Lists current commands.0
    new - Alows you to type a new password.1

    Here's my snippet of 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.


  2. #2
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Missing elements in array

    I have a double array
    No you don't - that would be declared
    double[] myDoubleArray
    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
    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]);

  3. #3
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    963
    Thanks
    7
    Thanked 205 Times in 181 Posts

    Default 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.

    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);
    }
    Last edited by pbrockway2; February 10th, 2012 at 04:37 PM.

  4. #4
    Member
    Join Date
    Jan 2011
    Posts
    88
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Missing elements in array

    Quote Originally Posted by pbrockway2 View Post
    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.

    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:
    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:
    3
    3

    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?

  5. #5
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    963
    Thanks
    7
    Thanked 205 Times in 181 Posts

    Default 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.

  6. #6
    Member
    Join Date
    Jan 2011
    Posts
    88
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default 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?

  7. #7
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    963
    Thanks
    7
    Thanked 205 Times in 181 Posts

    Default 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.

Similar Threads

  1. Help camparing array elements
    By Richmond in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 8th, 2012, 12:23 AM
  2. Array elements comparison
    By Pradeep_Parihar in forum Java Theory & Questions
    Replies: 1
    Last Post: December 10th, 2011, 09:45 AM
  3. java: adding array elements
    By dambundos in forum Collections and Generics
    Replies: 3
    Last Post: November 4th, 2011, 07:30 AM
  4. Comparing similar elements in an array
    By FJIW in forum Algorithms & Recursion
    Replies: 2
    Last Post: September 25th, 2011, 11:22 AM
  5. Comparing elements of an arrayList with an array?
    By DudeJericho in forum Collections and Generics
    Replies: 2
    Last Post: April 21st, 2011, 01:39 PM