Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Declare an Array?

 
Manuel Diaz
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to declare an array like this, but I dont know how to take this into code.

list: array [0..64,0..8]of integer = ((0,0,0,0,0,0,0,0,0),
( 11,18,0,0,0,0,0,0,0), //1
( 12,17,19,0,0,0,0,0,0), //2
( 9,13,18,20,0,0,0,0,0), //3
( 10,14,19,21,0,0,0,0,0), //4
( 11,15,20,22,0,0,0,0,0), //5
( 12,16,21,23,0,0,0,0,0), //6
( 13,22,24,0,0,0,0,0,0), //7
( 14,23,0,0,0,0,0,0,0), //8
( 3,19,26,0,0,0,0,0,0), //9
( 4,20,25,27,0,0,0,0,0), //10
( 1,5,17,21,26,28,0,0,0), //11
( 2,6,18,22,27,29,0,0,0), //12
( 3,7,19,23,28,30,0,0,0), //13
( 4,8,20,24,29,31,0,0,0), //14
( 5,21,30,32,0,0,0,0,0), //15
( 6,22,31,0,0,0,0,0,0), //16
( 2,11,27,34,0,0,0,0,0), //17
( 1,3,12,28,33,35,0,0,0), //18
( 2,4,9,13,25,29,34,36,0), //19
( 3,5,10,14,26,30,35,37,0),//20
( 4,6,11,15,27,31,36,38,0),//21
( 5,7,12,16,28,32,37,39,0),//22
( 6,8,13,29,38,40,0,0,0), //23
( 7,14,30,39,0,0,0,0,0), //24
( 10,19,35,42,0,0,0,0,0), //25
( 9,11,20,36,41,43,0,0,0), //26
( 10,12,17,21,33,37,42,44,0), //27
( 11,13,18,22,34,38,43,45,0), //28
( 12,14,19,23,35,39,44,46,0), //29
( 13,15,20,24,36,40,45,47,0), //30
( 14,16,21,37,46,48,0,0,0), //31
( 15,22,38,47,0,0,0,0,0), //32
( 18,27,43,50,0,0,0,0,0), //33
( 17,19,28,44,49,51,0,0,0), //34
( 18,20,25,29,41,45,50,52,0), //35
( 19,21,26,30,42,46,51,53,0), //36
( 20,22,27,31,43,47,52,54,0), //37
( 21,23,28,32,44,48,53,55,0), //38
( 22,24,29,45,54,56,0,0,0), //39
( 23,30,46,55,0,0,0,0,0), //40
( 26,35,51,58,0,0,0,0,0), //41
( 25,27,36,52,57,59,0,0,0), //42
( 26,28,33,37,49,53,58,60,0), //43
( 27,29,34,38,50,54,59,61,0), //44
( 28,30,35,39,51,55,60,62,0), //45
( 29,31,36,40,52,56,61,63,0), //46
( 30,32,37,53,62,64,0,0,0), //47
( 31,38,54,63,0,0,0,0,0), //48
( 34,43,59,0,0,0,0,0,0), //49
( 33,35,44,60,0,0,0,0,0), //50
( 34,36,41,45,57,61,0,0,0), //51
( 35,37,42,46,58,62,0,0,0), //52
( 36,38,43,47,59,63,0,0,0), //53
( 37,39,44,48,60,64,0,0,0), //54
( 38,40,45,61,0,0,0,0,0), //55
( 39,46,62,0,0,0,0,0,0), //56
( 42,51,0,0,0,0,0,0,0), //57
( 41,43,52,0,0,0,0,0,0), //58
( 42,44,49,53,0,0,0,0,0), //59
( 43,45,50,54,0,0,0,0,0), //60
( 44,46,51,55,0,0,0,0,0), //61
( 45,47,52,56,0,0,0,0,0), //62
( 46,48,53,0,0,0,0,0,0), //63
( 47,54,0,0,0,0,0,0,0)) //64
;

I wonder, how do I do this in java code?.
 
Henry Wong
author
Marshal
Pie
Posts: 21405
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would recommend that you start with some reading about Arrays... But if you can't wait, here is a quick example on creating a two dimension int array.


Henry
[ May 05, 2005: Message edited by: Henry Wong ]
 
Manuel Diaz
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's pretty good, but what if I want to use something like this:

list: array [0..64,0..8]of integer.

I want 64 records of array, each one with 8 numbers inside of it. How can I do this?
 
Henry Wong
author
Marshal
Pie
Posts: 21405
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Manuel Diaz:
I want 64 records of array, each one with 8 numbers inside of it. How can I do this?


There is no syntax difference between a 64 x 8 array, and a 2 x 3 array. You just have more typing to do...

Can't you figure it out from the example? BTW, I really suggest you read the tutorial on arrays.

Henry
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can declare the array size: Object[64][8] This is meaningful if you make an empty array. But since you are providing values in the declaration it will take the size from the values. I'm pretty sure. Try this for fun and illumination:

This says the array holds two items but I only provided one value. See if it held the second position with a null or if you get an index out of bounds exception. And let me know.

If you know the answer ... don't tell!
 
Henry Wong
author
Marshal
Pie
Posts: 21405
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I forgot to mention this yesterday... Did this question bring back memories for anyone else?

I haven't seen Pascal code in a long time. Of course, they call it Delphi now...

Henry
 
fred rosenberger
lowercase baba
Bartender
Posts: 12185
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but your example has 9 elements in each array. And, you have 65 arrays of arrays...
 
Manuel Diaz
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes I know, but I just presenting an example, I want that in java code. Can someone help me?.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. Change all the parentheses to braces, change the first line to

int twoDimArr[][] =

as Henry showed you, and then you're done. I'm not understanding why this is hard!
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, now I think I know what the issue is: you want, somehow, the Java array variable's type to include the size of the array -- is that it? In Java, array variables don't include any notion of dimensions. Even if you allocate an array at some fixed size -- i.e.,

int[][] array = new int[64][8];

there's nothing about the variable "array" that includes the notion of 64-ness or 8-ness, and you can later say

array = new int[2][2];
 
Manuel Diaz
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, and now how do I store all my values on the array, I have this, and the program output an error. Here is what I have.

-----------------------------------------------------------------
int[][] list = new int[5][8];
list [] [] = {0, 0, 0, 0, 0, 0, 0, 0, 0,
11, 18, 0, 0, 0, 0, 0, 0, 0, //1
12, 17, 19, 0, 0, 0, 0, 0, 0, //2
9, 13, 18, 20, 0, 0, 0, 0, 0, //3
10, 14, 19, 21, 0, 0, 0, 0, 0, //4
11, 15, 20, 22, 0, 0, 0, 0, 0, //5
------------------------------------------------------------------
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Manuel Diaz
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im getting an error on the brakets!!!. What can I do?
 
M Beck
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well, one thing you can do is show us the error. that would make it easier to help you solve it.
 
Manuel Diaz
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have troubles with all the values, but.. when I only input one, no error. For example this is what I have:

int[][] list ={{0, 0, 0, 0, 0, 0, 0, 0, 0,}};


if I try to make this I have an error:
int[][] list = {{0, 0, 0, 0, 0, 0, 0, 0, 0,}{11, 18, 0, 0, 0, 0, 0, 0, 0
};

WHY IS THIS HAPPENING TO ME??.
 
Manuel Diaz
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OH don't worry, I already found the error, thanks you all for your support.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic