954,523 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Arrays and assigning a string to a position

Hey guys,
Im new to Java and i am trying to wrap my head around arrays and what not. What i am trying to do is create an array for a puzzle which is 4x4 and has the numbers 1 to 16 so far i have got this

public class Puzzle {

    public static void main(String[] args) {

    Puzzle[][] puzzle = new Puzzle[4][4];
    // print array in rectangular form
    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 4; j++) {
            System.out.print(1+j);
        }
        System.out.println("");
            }
    }
}


which gives the output of text
1234
1234
1234
1234

so what i want to be able to do is have:
1,2,3,4
5,6,7,8
9,10,11,12
13,14,15,16

so not sure how to assign the number to the array positions

SurrounD
Newbie Poster
2 posts since Aug 2011
Reputation Points: 10
Solved Threads: 0
 
puzzle[i][j]


For accessing specific index u have to specify its index location...

in loop it will print
puzzle[0][0]
then
puzzle[0][1]
and so on....

Majestics
Practically a Master Poster
621 posts since Jul 2007
Reputation Points: 199
Solved Threads: 49
 

Two ways:
1: Do some arithmetic using i and j
the first row is 1,2,3,4
The second is 5,6,7,8 which is also 4+1, 4+2, 4+3, 4+4
The third is ... 8+1, 8+2 etc see the pattern?
2: you have a nested loop that you can use to access all the array elements in order (see Majestic's post as well). You need to put 1 in the first, 2 in the second... 16 in the last. You can do this with a simple "counter" variable that starts at 1 and adds 1 each time you use it.

JamesCherrill
Posting Genius
Moderator
6,371 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

This line

Puzzle[][] puzzle = new Puzzle[4][4];

means that each cell in the 4x4 grid is a "Puzzle". So you have 16 "Puzzle" instances. This may not be what you want. You want to have a 4x4 grid of something. What is the thing you want for each cell in the 4x4 grid?

JeffGrigg
Posting Whiz in Training
218 posts since Aug 2011
Reputation Points: 192
Solved Threads: 28
 

What you want is...

System.out.print(i*4+j+1);
hfx642
Posting Pro
515 posts since Nov 2009
Reputation Points: 248
Solved Threads: 105
 

What you want is...

System.out.print(i*4+j+1);


No. What they want is to learn Java, not just get their homework done for them ready to
copy/paste. Why do you think I went to such lengths to get the OP to work that out for themself?

JamesCherrill
Posting Genius
Moderator
6,371 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

a) It's not a java problem they are having... It's a logic problem.
b) I think that your explanation was rather long and would only confuse them even more. Once someone can see an example in a simple program, they will understand how to use it in more complex applications.
It's not like I did their complete assignment. Gee!!

hfx642
Posting Pro
515 posts since Nov 2009
Reputation Points: 248
Solved Threads: 105
 

Thanks for your help guys. Actually got it sorted myself

SurrounD
Newbie Poster
2 posts since Aug 2011
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: