Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 当我不能初始化为null时,如何初始化数组?_Java_Arrays_Nullpointerexception - Fatal编程技术网

Java 当我不能初始化为null时,如何初始化数组?

Java 当我不能初始化为null时,如何初始化数组?,java,arrays,nullpointerexception,Java,Arrays,Nullpointerexception,我有一个字符串数组,我想将其转换为int,非常简单和直接,下面是代码: public static void main(String[] args) { String myarray[]=readfile("[pathtothefile]"); int mynums[] = new int[myarray.length]; for (int i=0;i<myarray.length;i++){ mynums[i]=Integer.parseInt

我有一个字符串数组,我想将其转换为int,非常简单和直接,下面是代码:

public static void main(String[] args) {
    String myarray[]=readfile("[pathtothefile]");

     int mynums[] = new int[myarray.length];
    for (int i=0;i<myarray.length;i++){
        mynums[i]=Integer.parseInt(myarray[i]);
    }
    System.out.print(Arrays.toString(mynums));  
}
我要做的是解决这个问题

int mynums[] = new int[myarray.length]; 

有人解释了为什么会发生这种情况,但我现在不知道如何初始化!我的意思是,有时候我不知道我的数组能有多大,我只是想初始化它。甚至可能吗?

在Java中,一切都是幕后的指针。因此,当您执行mynums[]=null时,您指向的是null。那么什么是空[i]?这就是你的NPE的来源。或者,当您将它指向一个数组时,您实际上正在访问数组的第i个元素。

您必须首先初始化数组,因为它根据数组大小分配内存。例如,当您想向数组中添加一个整数时,它会将整数写入先前分配的内存中

内存大小不会随着您添加更多项而变大。(除非您使用列表或哈希映射,…但对于通用数组,情况并非如此)


如果你不知道你的数组有多大,请考虑使用<代码> SpulsInTalay。它类似于列表,并且随着您添加项而变大。

简单地说,在java中,数组是一个对象,因此您需要将其视为一个对象,并在对其进行任何操作之前对其进行初始化。

如果您不使用数组而使用ArrayList怎么办?它会随着元素的添加而动态增长。

这里有一个想法。当您将某个东西初始化为null时,您只是简单地声明它存在。例如如果我告诉你有只狗,但我什么也没告诉你。。。我没有告诉你它在哪里,有多高,多大,男/女等等。。。我没有告诉你它的任何属性,也没有告诉你如何访问它,我只告诉你有一只狗(为了便于讨论,它的名字叫Array),那么你就知道这些了。有一只狗的名字叫Array,就是它

通常,当大小已知且数据通常是不可变的时,使用数组。对于要更改的数据,应该使用ArrayList之类的工具。可随意更改;您可以随心所欲地添加/删除元素。有关ArrayList的更多信息,请阅读上面的链接

现在,关于您的代码:

public static void main(String[] args) {

    ArrayList<int> myInts = new ArrayList<int>();
      // define a new null arraylist of integers.

    // I'm going to assume that readfile() is a way for you get the file
    // into myarray. I'm not quite sure why you would need the [], but I'll
    // leave it.

    String myarray[] = readfile("[pathtothefile]");

    for (int i = 0; i < myarray.length; i++) {
             //adds the value you've specifed as an integer to the arraylist.
        myInts.add(Integer.parseInt(myarray[i])); 
 }

    for (int i = 0; i < myInts.size(); i++) {
            //print the integers
        System.out.print(Integer.toString(myInts.get(i)));
    }

}
publicstaticvoidmain(字符串[]args){
ArrayList myInts=新的ArrayList();
//定义一个新的空整数数组列表。
//我假设readfile()是获取文件的一种方法
//进入myarray。我不太确定您为什么需要[],但我会
//别管了。
字符串myarray[]=readfile(“[pathtothefile]”);
for(int i=0;i
反过来看:为什么定义空数组的索引是有意义的?在数组本身未初始化的情况下,如何访问数组中的特定项?另一种方式是将其视为装满杯子。如果你的杯子(阵列)不存在,你怎么可能向其中加水?首先,你必须有一个确定的杯子才能加水。当你说int mynums[]=null时,你声明了一个变量,它可以引用一个整数值数组,但在那一点上你说它什么都没有引用,所以如果它什么都没有引用,那么表达式mynums[i]就没有意义了。“我的意思是,有时候我不知道我的数组能有多大,我只想初始化它。甚至有可能“谢谢,那么,当我不知道初始数组的大小时,我如何解决这个问题呢?@user1972584您可以使用。查找列表,或者您必须读取文件两次。一次获取大小,一次填充数组。当然,列表可能是更好的选择(或ArrayList或Vector)。你也可以在阅读完后执行List.ToArray。@Smit我在某个地方看到ADT建议我使用SparseIntArray处理整数。@MiroMarkarian是的,但那是ADT。这家伙只需要一些基本的东西。SparseIntArray是一个大型优化工具,不是常规Java库的一部分,只是在android库中。SparseIntArray是一个仅在Android中可用,不属于JavaSE的一部分
public static void main(String[] args) {

    ArrayList<int> myInts = new ArrayList<int>();
      // define a new null arraylist of integers.

    // I'm going to assume that readfile() is a way for you get the file
    // into myarray. I'm not quite sure why you would need the [], but I'll
    // leave it.

    String myarray[] = readfile("[pathtothefile]");

    for (int i = 0; i < myarray.length; i++) {
             //adds the value you've specifed as an integer to the arraylist.
        myInts.add(Integer.parseInt(myarray[i])); 
 }

    for (int i = 0; i < myInts.size(); i++) {
            //print the integers
        System.out.print(Integer.toString(myInts.get(i)));
    }

}