Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java 3D array assign values

Tags:

java

arrays

3d

I have an array that looks like this

static String[][][] School= new String[1000][20][5]; 
  • In the first bracket I save the class name
  • In the second I save an ID of a student
  • In the third I save information about the student (his name, family name etc).

First I assign all the class names, after that I assign to every class its student ID and then I can fill in their information.

How can I do it? I tried it with for example

School[i] = "A1";

but it's not working.

EDIT: Or is there an other way to save this all 3 things? (class name, its students and its iformation)

like image 939
user3549340 Avatar asked May 13 '14 12:05

user3549340


2 Answers

static String[][][] School= new String[1000][20][5]; 

Consider figure which has 3 Dimension.

So when you insert School[0][0][0]="A1" it means you have entered element at 0,0,0 position.

From 0,0,0 this will move upto the position 1000,20,5.

You can insert like this But you have so many elements.

School[0][0][0]="A1"
School[0][0][1]="A2"
School[0][0][2]="A3"
.....
School[0][1][0]="B1"
School[0][1][1]="B2"
School[0][1][2]="B3"
......

In 3D array elements look like

int[3][4][2] array3D
// means Three (4x2) 2 Dimensional Arrays 

 int[4][2]
 //means Four 1 dimensional arrays.

Now how to add elements in 3D array?

At Start you can directly use

int[][][] threeDArray = 
    {  { {1,   2,  3}, { 4,  5,  6}, { 7,  8,  9} },
       { {10, 11, 12}, {13, 14, 15}, {16, 17, 18} },
       { {19, 20, 21}, {22, 23, 24}, {25, 26, 27} } };

This is very tedious task in your case as you want to insert details at every position. As you have 1000 records.

Your array will have elements like this

enter image description here

NOTE:It's not recommended to use 3D array for this purpose.

Suggestion:Declare a class with three Strings create constructor with this three parameters and put getter and setters to get and set values via Objects

like image 63
akash Avatar answered Oct 16 '22 08:10

akash


I will suggest instead of using a 3D array, you shall create a Student Class that will hold all the information for a student and A Class for SchoolClass that will hold a list of students in the class and name of class and you can maintain an Array of SchoolClass to serve the purpose.

This way you will be able to manage it better.

Hope this helps

like image 3
Sanjeev Avatar answered Oct 16 '22 07:10

Sanjeev