سؤال

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)

هل كانت مفيدة؟

المحلول

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

نصائح أخرى

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

Your array won't do what you expect it to do.

Think of the array like a 3D array, with each element a point. If you specify a single index, you're essentially telling the computer "OK, I want to assign "A1" to this slice of the array (in your example, you're trying to do something akin to String[][] elementAtI = "A1";). Now that doesn't make sense, does it?

To get to a single element in the array, you have to specify all three indices, much like how in 3D space you have to specify all three coordinates to locate a point:

School[3][4][5] = "A1";

What might be a better idea than a 3D array is objects. Packing everything into an array works, but that's not as readable as having a SchoolClass[], where each SchoolClass has a name and an array of Students, and each Student has an ID, name, etc.

First of all, variable fields usually start with lowercase with camel text by convention.

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

Secondly, arrays do not work like this. String[][][] holds {{{entry...}entry...}entry...}. The entries may contain duplicates, which makes it an unfeasible method as you will get {"3A", "1", "PersonName"} and {"3A", "1", "DifferentPersonName"} within the same array. Each array dimension is holding additional dimensions, aka {"3A", {"1", {"PersonName"}, {"DifferentPersonName"}}}, so

School[i] = "A1"; is syntax error, because you must put String[][] in String[i][][]:

School[i] = {{"A1","PersonName"}};

I believe a solution here would be to use HashMaps. Repeated entries will overwrite each other. In this case, the code will be:

// The actual HashMap!
static final HashMap<String, HashMap<String, String>> school
=new HashMap<String, HashMap<String, String>>();

/**
 * How to use an entry using the above HashSet:
 * 
 * @param className The name of the class.
 * @param id The id.
 * @param details The details.
 */
void addEntry(final String className, final String id, final String details){
  HashMap<String, String>value=school.get(className);
  // If the class already exists, use it, otherwise make new HashMap.
  (value==null ? value = new HashMap<String, String>() : value)
  // Here is how to put in the value mapping.
  .put(id, details);
  // Put it back in. This updates the entry.
  school.put(value);
}

/**
 * How to get (iterate through) entries from the above HashSet:
 * 
 * @return An array of students in format "class id details"
 */
String[] getStudentsSet(){
  // This is an iterator.
  Iterator<Entry<String, HashMap<String, String>>>iterator=
      school.entrySet().iterator();
  Entry<String, HashMap<String, String>>next;
  String now;
  // This is for testing
  final ArrayList<String>list=new ArrayList<String>();
  while(iterator.hasNext()){
    // Load new class name
    now=(next=iterator.next()).getKey();
    Iterator<Entry<String, String>>iteratorlv2=next.entrySet().iterator();
    while(iterator.hasNext()){
      final Entry<String, String>entry=iterator.next();
      /* This is the student from class "now", id "entry.getKey()", and details "entry.getValue()"
       * Change this line to what you want, or what you would like to use entries for.
       */
      final String student=now+" "+entry.getKey()+" "+entry.getValue();
      // This is for testing
      list.add(student);
    }
  }
  // This is what prints to the console so you know this thing works.
  for(final String o:list) System.out.println(o);
  return list.toArray(new String[list.size()]);
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top