Array that has this kind of graphical illustration

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JanineXD
    New Member
    • Feb 2010
    • 18

    Array that has this kind of graphical illustration

    I basically need help getting this kind of illustration using Arrays:

    ex:


    3
    5 6
    9 0 2
    3 4 1 4
    3 1 2 3 4

    the variables are just examples, I just need to know how can I do that without having it like this:

    3 0 0 0 0
    5 6 0 0 0
    9 0 2 0 0
    3 4 1 4 0
    3 1 2 3 4

    Thank You!!
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    Can we see how you're storing your arrays? eg are you actually creating an array of length 5, and storing the zeroes?

    If you only create an array of the size you want to output, it might make it easier; just use the .length property when outputting it.

    Comment

    • JanineXD
      New Member
      • Feb 2010
      • 18

      #3
      The zeros are just an example of the arrays.

      I just want to have the output like this:
      3
      5 6
      9 0 2
      3 4 1 4
      3 1 2 3 4

      can you teach me how can I get it without affecting the shape?

      thanks!

      Comment

      • jkmyoung
        Recognized Expert Top Contributor
        • Mar 2006
        • 2057

        #4
        Let's say you have an array of arrays,
        Code:
        int[][]array = new int[5][];
        int i,j;
        
        You initialize the arrays like so:
        for (i = 0; i < 5; i++){
        	array[i] = new int[i+1];
        }
        Then you set the values of the array somewhere. eg, array[0][0] = some value, array [1][0] = another value, etc... When you want to print it, use a nested for loop like so, which gets the length of each of the arrays:
        Code:
        for (i = 0; i < 5; i++){
        	int[] temp = array[i];
        	int len = temp.length;
        	for (j = 0; j < len; j++){
        		System.out.print(temp[j]+"\t");
        	}
        	System.out.println();
        }
        Don't know if you wanted arrays or a vector or something else. Eg, maybe you want to store all the number in the same array

        Comment

        • JanineXD
          New Member
          • Feb 2010
          • 18

          #5
          Thank you!

          How can I change the values because when i output its all zeros:

          0
          0 0
          0 0 0
          0 0 0 0
          0 0 0 0 0


          thanks again!

          Comment

          • jkmyoung
            Recognized Expert Top Contributor
            • Mar 2006
            • 2057

            #6
            Then you set the values of the array somewhere.
            eg,
            array[0][0] = 3;
            array [1][0] = 5;
            etc...

            Comment

            • JanineXD
              New Member
              • Feb 2010
              • 18

              #7
              thank you very much!

              Comment

              Working...