Hi working on a uni exercise and need to create a multidimensional array to hold some strings. But i'm a bit confused on how to access / declare the variables because how do i make the program know when i'm assigning variables which dimension i'm referring to? The end goal is create a 3 by 3 array but i assume the logic is the same as 2 by 2 but i just need to know how to access the arrays i want to. Thanks in advance :)
String[][] Albums = new String[5][10];
String song1,song2,song3;
String album1,album2,album3;
album1 = "Best classic hits";
album2 = "Best pop hits";
album3 = "Best rock hits";
song1 = "Greatest funk song";
song2 = "Greatest pop song";
song3 = "Greatest rock song";
Albums[0][0] = album1;
Albums[1][0] = album2;
Albums[2][0] = album3;
First correct this:
// To make the size 3 by 3
String[][] Albums = new String[3][3];
Then you can access each element like this:
for(int i = 0; i < Albums.length; i++) {
for(int j = 0; j < Albums[i].length; j++ ) {
System.out.println(Albums[i][j]);
}
}
Well you are creating a 5x10 array, you want to access the array to print the elements ? just create a nested for loop
like this
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
System.out.print(Number[i][j]+"\t");
}
}
Edit reading your comment is this what you want ?
public static void main(String[] args)
{
String[][] Album = {
{"Best classic hits ", "Best pop hits ", "Best rock hits "},
{"Greatest funk song" , "Greatest pop song" , "Greatest rock song"}
};
System.out.println(Album[0][0] + Album[1][0]);
System.out.println(Album[0][1] + Album[1][1]);
System.out.println(Album[0][2] + Album[1][2]);
}
Start by initializing the array to the desired size. Here you said you needed 3x3, so I assume the array looks like this:
[[album, song0, song1],
[album, song0, song1],
[album, song0, song1]]
Because representing an object (eg (album, song0, song1)
) by array is prone to error, I would suggest creating helper methods to manage the data structure access. These methods ensure you don't override the wrong array entry by mistake.
static void setData(String[] data, String album, String song0, String song1) {
setAlbum(data, album);
setSong0(data, song0);
setSong1(data, song1);
}
static String getAlbum(String[] data) {
return data[0];
}
static void setAlbum(String[] data, String album) {
data[0] = album;
}
static String getSong0(String[] data) {
return data[1];
}
static void setSong0(String[] data, String song0) {
data[1] = song0;
}
static String getSong1(String[] data) {
return data[2];
}
static void setSong1(String[] data, String song1) {
data[2] = song1;
}
...
String[][] data = new String[3][3];
setData(data[0], "album 0", "album 0 song 0", "album 0 song 1");
setData(data[1], "album 1", "album 1 song 0", "album 1 song 1");
setData(data[2], "album 2", "album 2 song 0", "album 2 song 1");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.