Any better way to populate this Array element values?

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 828 Views - Last Post: 17 August 2012 - 03:54 AM Rate Topic: -----

#1 JJu-256  Icon User is offline

  • D.I.C Head

Reputation: -6
  • View blog
  • Posts: 115
  • Joined: 21-December 11

Any better way to populate this Array element values?

Posted 16 August 2012 - 02:41 AM

public class ArrayOfarr {     
    public static void main(String[] args) {     
             
        System.out.println("Array1 \n");     
             
        byte[][] array1 = new byte[16][16];     
             
        array1[0][0] = 0;     
        array1[0][1] = 1; 
        array1[0][2] = 2;
        array1[0][3] = 3;
        array1[0][4] = 4;     
        array1[0][5] = 5; 
        array1[0][6] = 6;
        array1[0][7] = 7;
        array1[0][8] = 8;     
        array1[0][9] = 9; 
        array1[0][10] = 10;
        array1[0][11] = 11;
        array1[0][12] = 12;     
        array1[0][13] = 13; 
        array1[0][14] = 14;
        array1[0][15] = 15;
        array1[1][0] = 16;     
        array1[1][1] = 17;
        array1[1][2] = 18;     
        array1[1][3] = 19; 
        array1[1][4] = 20;
        array1[1][5] = 21;
        array1[1][6] = 22;     
        array1[1][7] = 23; 
        array1[1][8] = 24;
        array1[1][9] = 25;
        array1[1][10] = 26;     
        array1[1][11] = 27; 
        array1[1][12] = 28;
        array1[1][13] = 29;
        array1[1][14] = 30;     
        array1[1][15] = 31;     
        array1[2][0] = 32;     
        array1[2][1] = 33; 
        array1[2][2] = 34;     
        array1[2][3] = 35; 
        array1[2][4] = 36;
        array1[2][5] = 37;
        array1[2][6] = 38;     
        array1[2][7] = 39; 
        array1[2][8] = 40;
        array1[2][9] = 41;
        array1[2][10] = 42;     
        array1[2][11] = 43; 
        array1[2][12] = 44;
        array1[2][13] = 45;
        array1[2][14] = 46;     
        array1[2][15] = 47;
             
        for(int l=0; l < array1.length; l++){     
            for(int c=0; c < 16; c++){     
                System.out.print(array1[l][c] + " ");     
            }     
            System.out.println("\n");     
        }     
             
        System.out.println("Array2 \n");     
             
        byte[][] array2 = new byte[3][2];     
     
        /* Now allocate the columns     
        array2[0] = new byte[2];     
        array2[1] = new byte[2];     
        array2[2] = new byte[2];     
        */     
        array2[0][0] = 0;     
        array2[0][1] = 1;     
             
        array2[1][0] = 2;     
        array2[1][1] = 3;     
             
        array2[2][0] = 4;     
        array2[2][1] = 5;     
             
        for(int l=0; l < array2.length; l++){     
            for(int c=0; c < 2; c++){     
                System.out.print(array2[l][c] + " ");     
            }     
            System.out.println("\n");     
        }     
    }     
}    


Is This A Good Question/Topic? 0
  • +

Replies To: Any better way to populate this Array element values?

#2 m_wylie85  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 96
  • View blog
  • Posts: 899
  • Joined: 15-October 10

Re: Any better way to populate this Array element values?

Posted 16 August 2012 - 02:48 AM

You could populate the array with a for loop, similar to the way you are outputting the values
Was This Post Helpful? 0
  • +
  • -

#3 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2674
  • View blog
  • Posts: 11,293
  • Joined: 20-September 08

Re: Any better way to populate this Array element values?

Posted 16 August 2012 - 02:52 AM

Try


        byte val = 0;

        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < array1[i].length; j++) {
                array1[i][j] = val++;
            }
        }


Was This Post Helpful? 0
  • +
  • -

#4 JJu-256  Icon User is offline

  • D.I.C Head

Reputation: -6
  • View blog
  • Posts: 115
  • Joined: 21-December 11

Re: Any better way to populate this Array element values?

Posted 16 August 2012 - 03:52 AM

View Postm_wylie85, on 16 August 2012 - 02:48 AM, said:

You could populate the array with a for loop, similar to the way you are outputting the values


I had that going but if you noticed I still have to put in the value for each element. I don't want it that way, because it is redundantly annoying and time consuming. I will like to display 0-255 as values with an byte [][] array = new byte [16][16]. After which I can morph or transform them to something else like characters.

Thanks in advance for any idea you can come up with.
Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5795
  • View blog
  • Posts: 12,627
  • Joined: 16-October 07

Re: Any better way to populate this Array element values?

Posted 16 August 2012 - 03:56 AM

Use methods. Both to print and to fill.

public class ArrayOfarr {
	private byte[][] fill(byte[][] a) {
		byte n=0;
		for(int i=0; i < a.length; i++) {
			for(int j=0; j<a[i].length; j++) {
				a[i][j] = n++;
			}
		}
		return a;
	}
	
	private void print(byte[][] a) {
		for(int i=0; i < a.length; i++) {
			for(int j=0; j<a[i].length; j++) {
				System.out.print(a[i][j] + " ");
			}
			System.out.println("\n");
		}
	}
	

	public void run() {
		System.out.println("Array1 \n");
		byte[][] array1 = fill(new byte[16][16]);
		print(array1);

		System.out.println("Array2 \n");     
		byte[][] array2 = fill(new byte[3][2]);
		print(array2);
	}     
	
	public static void main(String[] args) {
		new ArrayOfarr().run();
	}
}



Also, "l"(L) is a bad variable name choice. Often looks like one 1(one).

Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#6 JJu-256  Icon User is offline

  • D.I.C Head

Reputation: -6
  • View blog
  • Posts: 115
  • Joined: 21-December 11

Re: Any better way to populate this Array element values?

Posted 16 August 2012 - 04:49 AM

View Postg00se, on 16 August 2012 - 02:52 AM, said:

Try


        byte val = 0;

        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < array1[i].length; j++) {
                array1[i][j] = val++;
            }
        }


I tried you method and this is what I get:

Array2

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47

48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63

64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79

80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95

96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111

112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127

-128 -127 -126 -125 -124 -123 -122 -121 -120 -119 -118 -117 -116 -115 -114 -113

-112 -111 -110 -109 -108 -107 -106 -105 -104 -103 -102 -101 -100 -99 -98 -97

-96 -95 -94 -93 -92 -91 -90 -89 -88 -87 -86 -85 -84 -83 -82 -81

-80 -79 -78 -77 -76 -75 -74 -73 -72 -71 -70 -69 -68 -67 -66 -65

-64 -63 -62 -61 -60 -59 -58 -57 -56 -55 -54 -53 -52 -51 -50 -49

-48 -47 -46 -45 -44 -43 -42 -41 -40 -39 -38 -37 -36 -35 -34 -33

-32 -31 -30 -29 -28 -27 -26 -25 -24 -23 -22 -21 -20 -19 -18 -17

-16 -15 -14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1

See the code below:
public class ArrayOfarr {     
    public static void main(String[] args) {     
             
        System.out.println("Array1 \n");     
             
        byte[][] array1 = new byte[16][16];     
             
        array1[0][0] = 0;     
        array1[0][1] = 1; 
        array1[0][2] = 2;
        array1[0][3] = 3;
        array1[0][4] = 4;     
        array1[0][5] = 5; 
        array1[0][6] = 6;
        array1[0][7] = 7;
        array1[0][8] = 8;     
        array1[0][9] = 9; 
        array1[0][10] = 10;
        array1[0][11] = 11;
        array1[0][12] = 12;     
        array1[0][13] = 13; 
        array1[0][14] = 14;
        array1[0][15] = 15;
        array1[1][0] = 16;     
        array1[1][1] = 17;
        array1[1][2] = 18;     
        array1[1][3] = 19; 
        array1[1][4] = 20;
        array1[1][5] = 21;
        array1[1][6] = 22;     
        array1[1][7] = 23; 
        array1[1][8] = 24;
        array1[1][9] = 25;
        array1[1][10] = 26;     
        array1[1][11] = 27; 
        array1[1][12] = 28;
        array1[1][13] = 29;
        array1[1][14] = 30;     
        array1[1][15] = 31;     
        array1[2][0] = 32;     
        array1[2][1] = 33; 
        array1[2][2] = 34;     
        array1[2][3] = 35; 
        array1[2][4] = 36;
        array1[2][5] = 37;
        array1[2][6] = 38;     
        array1[2][7] = 39; 
        array1[2][8] = 40;
        array1[2][9] = 41;
        array1[2][10] = 42;     
        array1[2][11] = 43; 
        array1[2][12] = 44;
        array1[2][13] = 45;
        array1[2][14] = 46;     
        array1[2][15] = 47;
             
        for(int l=0; l < array1.length; l++){     
            for(int c=0; c < 16; c++){     
                System.out.print(array1[l][c] + " ");     
            }     
            System.out.println("\n");     
        }     
             
        System.out.println("Array2 \n");     
             
        byte[][] array2 = new byte[16][16];     
     
        /*This part is a control I will later change my code with this as it is compact
        Now allocate the columns     
        array2[0] = new byte[2];     
        array2[1] = new byte[2];     
        array2[2] = new byte[2];     
             
        array2[0][0] = 0;     
        array2[0][1] = 1;     
             
        array2[1][0] = 2;     
        array2[1][1] = 3;     
             
        array2[2][0] = 4;     
        array2[2][1] = 5; 
        */
        byte val =0;     
        for(int l=0; l < 16; l++){     
            for(int c=0; c <array2[l].length; c++){
            	array2[l][c]= val++;
               System.out.print(array2[l][c] + " ");     
            }     
            System.out.println("\n");     
        }     
    }     
 
}


Was This Post Helpful? 0
  • +
  • -

#7 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2674
  • View blog
  • Posts: 11,293
  • Joined: 20-September 08

Re: Any better way to populate this Array element values?

Posted 16 August 2012 - 04:56 AM

Quote

I tried you method and this is what I get:


You didn't ;) Because my method, like yours, only fills the array up to and including the value 47

If otoh, you altered my method to fill the whole array, that's what you would get, which is correct.

This post has been edited by g00se: 16 August 2012 - 04:58 AM
Reason for edit:: qualified

Was This Post Helpful? 0
  • +
  • -

#8 JJu-256  Icon User is offline

  • D.I.C Head

Reputation: -6
  • View blog
  • Posts: 115
  • Joined: 21-December 11

Re: Any better way to populate this Array element values?

Posted 16 August 2012 - 05:03 AM

View Postg00se, on 16 August 2012 - 04:56 AM, said:

Quote

I tried you method and this is what I get:


You didn't ;) Because my method, like yours, only fills the array up to and including the value 47

If otoh, you altered my method to fill the whole array, that's what you would get, which is correct.


How then do I get read of the negative numbers? I wanted it to go all the way to 255.
Was This Post Helpful? 0
  • +
  • -

#9 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1946
  • View blog
  • Posts: 4,047
  • Joined: 11-December 07

Re: Any better way to populate this Array element values?

Posted 16 August 2012 - 05:24 AM

byte goes from -128 to 127. If you want to go outside that range then use a different data type. Unless you have a good reason, it's good practice to use int in Java rather than byte, short or long. A lot of reasons that seem like a good idea at the time often turn out to be micro-optimisations that cost you in the long run.
Was This Post Helpful? 0
  • +
  • -

#10 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2674
  • View blog
  • Posts: 11,293
  • Joined: 20-September 08

Re: Any better way to populate this Array element values?

Posted 16 August 2012 - 05:25 AM

Quote

I wanted it to go all the way to 255.


It can't. A byte is a signed datatype and its maximum value is the one you see in your display. If you want to display it as if it were unsigned, you need to do

System.out.println(0xFF & array1[i][j]);


If you want to store that value, then you should use char, int, or long (ascending order of width)
Was This Post Helpful? 0
  • +
  • -

#11 JJu-256  Icon User is offline

  • D.I.C Head

Reputation: -6
  • View blog
  • Posts: 115
  • Joined: 21-December 11

Re: Any better way to populate this Array element values?

Posted 16 August 2012 - 05:34 AM

You mean like so


  char val =0;     
        for(int l=0; l < 16; l++){     
            for(int c=0; c <array2[l].length; c++){
            	array2[l][c]= char++;
               System.out.print(array2[l][c] + " ");     
            }     
            System.out.println("\n");     
        }   



Was This Post Helpful? 0
  • +
  • -

#12 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2674
  • View blog
  • Posts: 11,293
  • Joined: 20-September 08

Re: Any better way to populate this Array element values?

Posted 16 August 2012 - 05:37 AM

That should be val++, not char++

btw, never use the letter 'L' (in either case) for a variable name

This post has been edited by g00se: 16 August 2012 - 05:38 AM
Reason for edit:: L

Was This Post Helpful? 0
  • +
  • -

#13 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1946
  • View blog
  • Posts: 4,047
  • Joined: 11-December 07

Re: Any better way to populate this Array element values?

Posted 16 August 2012 - 05:43 AM

All that said, it's all just bits. The way Java overflows are implemented, it's straightforward to pretend that the primitives are unsigned. The demo below shows this, although I wouldn't recommend using it in practice.

package cfoley.gtd.gui;

public class ByteArrMethods {
	
	public static void main(String[] args) {
		byte[][] arr = populate(new byte[16][16]);
		print(arr);
		System.out.println();
		printUnsigned(arr);
		
	}
	
	public static byte[][] populate(byte[][] arr) {
		byte value = 0;
		for(int i = 0; i < arr.length; i++) {
			for(int j = 0; j < arr[i].length; j++) {
				arr[i][j] = value++; 
			}
		}
		return arr;
	}
	
	public static void print(byte[][] arr) {
		for(byte[] line : arr) {
			for(byte b : line) {
				System.out.print(b + "\t");
			}
			System.out.println();
		}
	}
	
	public static void printUnsigned(byte[][] arr) {
		for(byte[] line : arr) {
			for(byte b : line) {
				System.out.print((b << 24 >>> 24) + "\t");
			}
			System.out.println();
		}
	}

}



The observant will notice that I end up using int values to get the display right anyway.
Was This Post Helpful? 0
  • +
  • -

#14 JJu-256  Icon User is offline

  • D.I.C Head

Reputation: -6
  • View blog
  • Posts: 115
  • Joined: 21-December 11

Re: Any better way to populate this Array element values?

Posted 16 August 2012 - 12:35 PM

View Postcfoley, on 16 August 2012 - 05:43 AM, said:

All that said, it's all just bits. The way Java overflows are implemented, it's straightforward to pretend that the primitives are unsigned. The demo below shows this, although I wouldn't recommend using it in practice.

package cfoley.gtd.gui;

public class ByteArrMethods {
	
	public static void main(String[] args) {
		byte[][] arr = populate(new byte[16][16]);
		print(arr);
		System.out.println();
		printUnsigned(arr);
		
	}



Thank you very much for your help here. I am going to ask you why you won't use this method in a practice. What is wrong with this method? My next move now is to make this element hold hex representation of the ASCII and extended ASCII. How possible can I achieve that? When all is said and done I can successfully transform the ASCII to what element remains after the known 128 ASCIIs.
	
	public static byte[][] populate(byte[][] arr) {
		byte value = 0;
		for(int i = 0; i < arr.length; i++) {
			for(int j = 0; j < arr[i].length; j++) {
				arr[i][j] = value++; 
			}
		}
		return arr;
	}
	
	public static void print(byte[][] arr) {
		for(byte[] line : arr) {
			for(byte b : line) {
				System.out.print(b + "\t");
			}
			System.out.println();
		}
	}
	
	public static void printUnsigned(byte[][] arr) {
		for(byte[] line : arr) {
			for(byte b : line) {
				System.out.print((b << 24 >>> 24) + "\t");
			}
			System.out.println();
		}
	}

}



The observant will notice that I end up using int values to get the display right anyway.

Was This Post Helpful? 0
  • +
  • -

#15 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1946
  • View blog
  • Posts: 4,047
  • Joined: 11-December 07

Re: Any better way to populate this Array element values?

Posted 16 August 2012 - 02:07 PM

Just because a piece of code works doesn't mean it is good. In this case, it has poor readability by breaking a lot of expectations of the programmer. In Java, everyone knows that the primitive data types are signed. By treating them as unsigned you have to jump through a lot of hoops to make them behave, and a lot of that code will look like it should be buggy when it is actually OK.

Then there is the problem of having to use the bit shift operators. They aren't commonly used in Java and, in fact, I had to look them up to write that bit of code. Anyone with a background in c will know them but I've been programming in Java for over a decade and I've never used them in any production code. I'm sure I'm not alone.

None of this is a terrible problem per se. You can write comments to explain what you have dome and encapsulate the worst bits in methods. But why go to that trouble when you can just use an int.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2