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");
}
}
}
17 Replies - 514 Views - Last Post: 17 August 2012 - 03:54 AM
#1
Any better way to populate this Array element values?
Posted 16 August 2012 - 02:41 AM
Replies To: Any better way to populate this Array element values?
#2
Re: Any better way to populate this Array element values?
Posted 16 August 2012 - 02:48 AM
#3
Re: Any better way to populate this Array element values?
Posted 16 August 2012 - 02:52 AM
byte val = 0;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < array1[i].length; j++) {
array1[i][j] = val++;
}
}
#4
Re: Any better way to populate this Array element values?
Posted 16 August 2012 - 03:52 AM
m_wylie85, on 16 August 2012 - 02:48 AM, said:
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.
#5
Re: Any better way to populate this Array element values?
Posted 16 August 2012 - 03:56 AM
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.
#6
Re: Any better way to populate this Array element values?
Posted 16 August 2012 - 04:49 AM
g00se, on 16 August 2012 - 02:52 AM, said:
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");
}
}
}
#7
Re: Any better way to populate this Array element values?
Posted 16 August 2012 - 04:56 AM
Quote
You didn't
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
#8
Re: Any better way to populate this Array element values?
Posted 16 August 2012 - 05:03 AM
g00se, on 16 August 2012 - 04:56 AM, said:
Quote
You didn't
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.
#9
Re: Any better way to populate this Array element values?
Posted 16 August 2012 - 05:24 AM
#10
Re: Any better way to populate this Array element values?
Posted 16 August 2012 - 05:25 AM
Quote
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)
#11
Re: Any better way to populate this Array element values?
Posted 16 August 2012 - 05:34 AM
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");
}
#12
Re: Any better way to populate this Array element values?
Posted 16 August 2012 - 05:37 AM
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
#13
Re: Any better way to populate this Array element values?
Posted 16 August 2012 - 05:43 AM
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.
#14
Re: Any better way to populate this Array element values?
Posted 16 August 2012 - 12:35 PM
cfoley, on 16 August 2012 - 05:43 AM, said:
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.
#15
Re: Any better way to populate this Array element values?
Posted 16 August 2012 - 02:07 PM
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.
|
|

New Topic/Question
Reply



MultiQuote






|