本文介绍了数组初始化时不能在声明语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以这样写:

  ACLASS []数组= {object1,Object2的}

我也可以这样写:

  ACLASS []数组=新ACLASS [2];
...
数组[0] = object1;
阵列[1] = Object2的;

但我不能写:

  ACLASS []数组;
...
阵列= {object1,Object2的};

这是为什么阻止Java的?

我知道如何解决它,但不时会比较简单。

例如:

 公共无效selectedPointsToMove(cpVect坐标){    如果(标签== NULL){
        如果(arePointsClose(坐标,点1,10)){
            cpVect [] = tempTab {}点1;
            标签= tempTab;
        }否则如果(arePointsClose(点2,坐标,10)){
            cpVect [] = tempTab {}点2;
            标签= tempTab;
        }其他{
            cpVect [] = tempTab {点1,点2};
            标签= tempTab;
        }
    }
}

这个简单的问题,因为我学会了如何使用数组在Java中发挥已经缠着我。


解决方案

You'd have to ask the Java designers. There might be some subtle grammatical reason for the restriction. Note that some of the array creation / initialization constructs were not in Java 1.0, and (IIRC) were added in Java 1.1.

But "why" is immaterial ... the restriction is there, and you have to live with it.

You can write this:

AClass[] array;
...
array = new AClass[]{object1, object2};

这篇关于数组初始化时不能在声明语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 20:21