Printing out a Random Array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • BigDaddyLH
    Recognized Expert Top Contributor
    • Dec 2007
    • 1216

    #16
    Why not use java.util.Array s? You're importing it:
    [CODE=java]import java.util.Array s;

    public class Compact {
    public static void main(String[] args) {
    int a[] = new int[20];
    for (int i = 0; i < a.length ; i++) {
    a[i] = (int)(Math.rand om() * 10);
    }
    System.out.prin tln("The random array is: "
    + Arrays.toString (a));
    }
    }[/CODE]

    The reason you were not getting the right output before is because the toString method for arrays doesn't output their contents.

    Comment

    • Laharl
      Recognized Expert Contributor
      • Sep 2007
      • 849

      #17
      Your code as it is will print out the memory address of the array, not the actual elements. The identifier on an array, when passed to System.out.prin tln() does not work like the identifier for a primitive type, such as an integer. Instead, it prints out the memory location.

      [CODE=java]
      System.out.prin tln(a); //Only prints a's address, since a is actually a pointer to the array

      for (int b = 0; b < a.length; b++) //Prints out each address on one line, then a newline
      System.out.prin t(a[b]+" ");
      System.out.prin tln(); //Newline
      [/CODE]

      Comment

      • Energizer100
        New Member
        • Nov 2007
        • 60

        #18
        Originally posted by Laharl
        Your code as it is will print out the memory address of the array, not the actual elements. The identifier on an array, when passed to System.out.prin tln() does not work like the identifier for a primitive type, such as an integer. Instead, it prints out the memory location.

        [CODE=java]
        System.out.prin tln(a); //Only prints a's address, since a is actually a pointer to the array

        for (int b = 0; b < a.length; b++) //Prints out each address on one line, then a newline
        System.out.prin t(a[b]+" ");
        System.out.prin tln(); //Newline
        [/CODE]
        I got it working, thanks

        Comment

        • Energizer100
          New Member
          • Nov 2007
          • 60

          #19
          Originally posted by Energizer100
          I got it working, thanks
          Okay, now that that is done. I have another problem.

          I want to get rid of all of the zeroes in the random array. For example, the output would look something like this.

          1 0 2 3 4 5 6 7 8 0 9 9 8 7 6 5 4 3 2 1 //Prints out the initial random array
          1 2 3 4 5 6 7 8 9 9 8 7 6 5 4 3 2 1 //Prints out the same array but without the zeroes.


          So here is my idea

          Code:
          import java.util.Arrays;
          
          public class Compact 
          {
          	public static void main(String args[])
          	{	
          		int a[] = new int[20];	
          
          		for (int i = 0; i < a.length; i++)
          			{
          				int b = (int) (Math.random() * 10);
          				a[i] = b;
          				System.out.print(a[i] + " ");
          			}
          		
          		for (int k = 0; k < a.length; k++)
          			{
          				if (a[k] == 0)
          				{
          					//remove a zero
          					int temp = a[k];
          				}
          			}	
          	}
          }
          So what I am trying to do is store all the zeroes in temp, then when i print out the random array without the zeroes, i'll just subtract temp from it.

          Comment

          • Laharl
            Recognized Expert Contributor
            • Sep 2007
            • 849

            #20
            There's a simpler way to do this. Simply use an if statement, saying if a[i]!=0, execute the printing code. That way, all the nonzero elements get printed, without any annoying removal code. If you really want to do that, I'd recommend an ArrayList.

            Comment

            • Energizer100
              New Member
              • Nov 2007
              • 60

              #21
              Originally posted by Laharl
              There's a simpler way to do this. Simply use an if statement, saying if a[i]!=0, execute the printing code. That way, all the nonzero elements get printed, without any annoying removal code. If you really want to do that, I'd recommend an ArrayList.

              It's an assignment, and I'm not allowed to use ArrayList

              Comment

              • Laharl
                Recognized Expert Contributor
                • Sep 2007
                • 849

                #22
                Selective printing (the if statement) is by far the easiest solution. If you actually have to remove the zeroes, this problem is far more difficult.

                Comment

                • Energizer100
                  New Member
                  • Nov 2007
                  • 60

                  #23
                  Originally posted by Laharl
                  Selective printing (the if statement) is by far the easiest solution. If you actually have to remove the zeroes, this problem is far more difficult.

                  I have to remove the zeros

                  Comment

                  • Laharl
                    Recognized Expert Contributor
                    • Sep 2007
                    • 849

                    #24
                    Given that arrays cannot be resized, you'll need a new array composed of the nonzero elements of the original. If you're limited to one array, you can push the zeroes to either end, then print either starting from the first nonzero or ending at the last end.

                    Comment

                    • Energizer100
                      New Member
                      • Nov 2007
                      • 60

                      #25
                      Originally posted by Laharl
                      Given that arrays cannot be resized, you'll need a new array composed of the nonzero elements of the original. If you're limited to one array, you can push the zeroes to either end, then print either starting from the first nonzero or ending at the last end.
                      oh. So i just move the zeroes to the end and print out the first non zero integer all the way to the last non zero integer? How do I do that? I was thinking about using a for loop and adding it to the last one. So when the for loop hits the last integer, all the zeros would go to the end.

                      Comment

                      • Laharl
                        Recognized Expert Contributor
                        • Sep 2007
                        • 849

                        #26
                        Kind of. When you find the first zero, you want to switch it with the last element. The second, the second to last element. And so on. To do this, you can keep track of how many you've found and access the appropriate element near the end to switch with (length-count, make sure you increment count at the right time). Note that if the element you're trying to switch with is a zero, you have to move to the next element towards the front until you have a nonzero to switch with.

                        Comment

                        • Energizer100
                          New Member
                          • Nov 2007
                          • 60

                          #27
                          Originally posted by Laharl
                          Kind of. When you find the first zero, you want to switch it with the last element. The second, the second to last element. And so on. To do this, you can keep track of how many you've found and access the appropriate element near the end to switch with (length-count, make sure you increment count at the right time). Note that if the element you're trying to switch with is a zero, you have to move to the next element towards the front until you have a nonzero to switch with.
                          wouldn't that just switch the array around?

                          for example

                          1 0 0 2 3 4 5 6 7 8 0 9 2 1 4 6 0 4 // regular array
                          1 4 0 2 3 4 5 6 7 8 6 9 2 1 4

                          Comment

                          • JosAH
                            Recognized Expert MVP
                            • Mar 2007
                            • 11453

                            #28
                            If you don't want zeros in your array, I'd say don't put them in there in the first place.

                            kind regards,

                            Jos

                            Comment

                            • Energizer100
                              New Member
                              • Nov 2007
                              • 60

                              #29
                              Originally posted by JosAH
                              If you don't want zeros in your array, I'd say don't put them in there in the first place.

                              kind regards,

                              Jos

                              well, its kind of an assignment. First I have to print out the random array, then i print out that array without zeros

                              Comment

                              • JosAH
                                Recognized Expert MVP
                                • Mar 2007
                                • 11453

                                #30
                                Originally posted by Energizer100
                                well, its kind of an assignment. First I have to print out the random array, then i print out that array without zeros
                                Simply iterate (loop) over the array; if you see a non-zero number then print it,
                                otherwise skip it (i.e. do nothing).

                                kind regards,

                                Jos

                                Comment

                                Working...