Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Java (http://www.go4expert.com/articles/java-tutorials/)
-   -   Multi-dimensional arrays in Java (http://www.go4expert.com/articles/multi-dimensional-arrays-java-t1162/)

mikkko 28Jul2011 21:11

Re: Multi-dimensional arrays in Java
 
pradeep, can you help me with this please

Create an addressbook program that asks for user input and could store until 15 entries using multi-dimensional array.

Note: Use JOptionPane or BufferedReader for the user input and program output.

Here is the sample format:
Name : Florence Santiago
Tel# : 735-1234
Address : Manila

Name : Joyce Jimenez
Tel# : 983-33333
Address : Quezon City

Name : Becca Singson
Tel# : 456-12345
Address : Pasig City

The number of entries to be displayed will be based on the number of addressbook entries inputted by the user.

finedesignnyc 28Jul2011 21:39

Re: Multi-dimensional arrays in Java
 
Does all of this work well with any version of Java?

weebo123 21Oct2011 20:40

Re: Multi-dimensional arrays in Java
 
Hi Pradeep,

I have recently decided to do some Android Game Development using Java. However I am having the below issue:

I am looking at creating a 2D birdseye view of a map where the user is basically working there way through it.

Is there a way that I can create the whole map in 1 go and store it somewhere. Then simply only display a certain section of that map during gameplay.
e.g map size =100 x 100 but user can only ever see 5x5 (which is zoomed tin to use up 70% of the screen).

I am thinking of using a 2d java array of bitmaps and using this array to draw onto a canvas. However, I am not entirly sure how to do this, as i would also like that every time the player moves their character forward a step, the next section of the canvas is drawn (always 5 x 5 regardless of whether they go left, right, straight or back).

Any help as to how I could go about doing this would be much appreciated.

Thanks

mjshaheed 20Dec2011 10:17

Re: Multi-dimensional arrays in Java
 
Hi Pradeep,

First of all sorry for digging up the old thread! I am learning Java...

I have a question as to how many objects are created in the following code:

Code:

int[][] a2 = new int[10][5];
You said "This allocates an int array with 10 rows and 5 columns. As with all objects, the values are initialized to zero (unlike local variables which are uninitialized).

This actually allocates 6 objects: a one-dimensional array of 5 elements for each of the rows, and a one-dimensional array of ten elements, with each element pointing to the appropriate row array
."

Aren't 10 object(references) created here, each for one row...a[0]....a[9]? Again sorry if i am mistaken!!!

Thanks,
Shahul

lis 28Mar2012 03:38

Re: Multi-dimensional arrays in Java
 
How to do an array in java?
I need to make an array that reads the latitude and longitude.
A for loop that reads into all of them into the array.

latitude Longitude
27.03434 -73.3423
27.02342 -73.2466
........... ...........
........... ...........
...this continues on
So I guess I need an array to read in the rows from the file without knowing the exact number of rows in the file.

double[][] numbers = new double [][];

for (double row = 0; row < numbers.length; row++)
{
for (double col = 0; col < numbers[row].length; col++)
System.out.println(numbers[row][col]);

Can you please help me. This won't work.


I have tried many ways, but can't figure it out.
There are two columns, but the rows of the files are a lot, and I need the array to read the numbers in the rows that are available into the array

mfred90 1Jun2012 18:02

Re: Multi-dimensional arrays in Java
 
To Create two dimensional array dunamically.I know the number of columns. but the number of rows are chnaged dynamically. I have tried array lisy. But it storing single value. so what should I Do ?

cuongedvhp 14Jun2012 12:05

Re: Multi-dimensional arrays in Java
 
There are two ways to implement 2-dimensional arrays. Many languages reserve a block of memory large enough to hold all elements of the full, rectangular, array (number of rows times number of columns times the element size). Java doesn't do this. Instead Java builds multi-dimensional arrays from many one-dimensional arrays, the so-called "arrays of arrays" approach. [C++ supports both styles.]

Healthcare 20Jun2012 12:09

Re: Multi-dimensional arrays in Java
 
for (int r=0; r<a2.length; r++) {
for (int c=0; c<a2[r].length; c++) {
System.out.print(" " + a2[r][c]);
}

Hi, Pradeep
I have a doubt in for loop. Can you explain why you have taken the statement "c<a2[r].length". I am little bit confused. please help.


All times are GMT +5.5. The time now is 10:10.