aspose file tools*
The moose likes Beginning Java and the fly likes Declare an Array? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Declare an Array?" Watch "Declare an Array?" New topic
Author

Declare an Array?

Manuel Diaz
Ranch Hand

Joined: Apr 22, 2005
Posts: 79
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?.


Note: I love programming.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19004
    
  40

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 ]

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Manuel Diaz
Ranch Hand

Joined: Apr 22, 2005
Posts: 79
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
Sheriff

Joined: Sep 28, 2004
Posts: 19004
    
  40

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

Joined: Jan 29, 2003
Posts: 8791
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!


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19004
    
  40

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

Joined: Oct 02, 2003
Posts: 11480
    
  16

but your example has 9 elements in each array. And, you have 65 arrays of arrays...


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Manuel Diaz
Ranch Hand

Joined: Apr 22, 2005
Posts: 79
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

Joined: Jul 08, 2003
Posts: 24187
    
  34

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!


[Jess in Action][AskingGoodQuestions]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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

Joined: Apr 22, 2005
Posts: 79
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

Joined: Jul 08, 2003
Posts: 24187
    
  34

Manuel Diaz
Ranch Hand

Joined: Apr 22, 2005
Posts: 79
Im getting an error on the brakets!!!. What can I do?
M Beck
Ranch Hand

Joined: Jan 14, 2005
Posts: 323
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

Joined: Apr 22, 2005
Posts: 79
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

Joined: Apr 22, 2005
Posts: 79
OH don't worry, I already found the error, thanks you all for your support.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Declare an Array?