【问题标题】:Java 3D array assign valuesJava 3D 数组赋值
【发布时间】:2014-05-13 12:00:48
【问题描述】:

我有一个看起来像这样的数组

static String[][][] School= new String[1000][20][5]; 
  • 在第一个括号中我保存类名
  • 第二次我保存了一个学生的 ID
  • 在第三部分中,我保存了有关学生的信息(他的姓名、姓氏等)。

首先我分配所有班级名称,然后为每个班级分配学生ID,然后我可以填写他们的信息。

我该怎么做?例如,我尝试过

School[i] = "A1";

但它不起作用。

编辑:或者还有其他方法可以保存这三件事吗? (班级名称、学生及其信息)

【问题讨论】:

标签: java arrays 3d


【解决方案1】:
static String[][][] School= new String[1000][20][5]; 

考虑具有 3 维的图形。

所以当您插入School[0][0][0]="A1" 时,这意味着您在 0,0,0 位置输入了元素。

从 0,0,0 向上移动到位置 1000,20,5。

你可以这样插入但是你有这么多元素。

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"
......

在 3D 数组中元素的样子

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

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

现在如何在 3D 数组中添加元素?

一开始就可以直接使用

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} } };

在您的情况下,这是一项非常繁琐的任务,因为您想在每个位置插入详细信息。 正如你有 1000 记录。

你的数组会有这样的元素

注意不建议为此使用 3D 阵列。

建议用三个Strings 声明一个类,用这三个参数创建构造函数,并通过Objects 放置getter 和setter 来获取和设置值

【讨论】:

  • 你能像 int[3][4][2] array3D 这样声明数组的维度吗?或者这是一个错误?如果可以,那么您可以在哪个版本或最高版本的 java 中执行此操作?
【解决方案2】:

我建议不要使用 3D 数组,而是创建一个 Student 类来保存学生的所有信息,并创建一个 SchoolClass 的类来保存班级中的学生列表和姓名类,您可以维护一个Array of SchoolClass 来达到目的。

这样你就能更好地管理它。

希望对你有帮助

【讨论】:

    【解决方案3】:

    你的数组不会做你期望它做的事情。

    将数组想象成一个 3D 数组,每个元素都有一个点。如果您指定单个索引,您实际上是在告诉计算机“好的,我想将 "A1" 分配给 数组的这个切片(在您的示例中,您正在尝试做类似的事情到String[][] elementAtI = "A1";)。现在这没有意义,不是吗?

    要获取数组中的单个元素,您必须指定所有三个索引,就像在 3D 空间中您必须指定所有三个坐标才能定位一个点:

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

    对象可能比 3D 数组更好。将所有内容打包到一个数组中是可行的,但这不像SchoolClass[]那样可读,每个SchoolClass都有一个name和一个Students数组,每个Student都有一个ID,@987654330 @等

    【讨论】:

      【解决方案4】:

      首先,变量字段按照惯例通常以小写字母开头。

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

      其次,数组不是这样工作的。 String[][][] 保存 {{{entry...}entry...}entry...}。 这些条目可能包含重复项,这使其成为一种不可行的方法,因为您将在同一数组中获得{"3A", "1", "PersonName"}{"3A", "1", "DifferentPersonName"}。每个数组维度都包含额外的维度,也就是 {"3A", {"1", {"PersonName"}, {"DifferentPersonName"}}},所以

      School[i] = "A1";是语法错误,因为你必须把String[][]放在String[i][][]中:

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

      我相信这里的解决方案是使用 HashMaps。重复的条目将相互覆盖。在这种情况下,代码将是:

      // 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()]);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-12-17
        • 2011-02-04
        • 2021-01-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多