Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/svn/5.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 如何在不获取NullPointException的情况下将数组的大小加倍?_Java_Arrays_Nullpointerexception - Fatal编程技术网

Java 如何在不获取NullPointException的情况下将数组的大小加倍?

Java 如何在不获取NullPointException的情况下将数组的大小加倍?,java,arrays,nullpointerexception,Java,Arrays,Nullpointerexception,首先,为了便于理解,这里是我昨天的帖子: 所以我得到了这个NullPointerException,我现在相信它是在我试图找到字符串数组中第一个重复项的索引之前发生的。在搜索第一个副本的索引之前,我使用以下方法将字符串数组的大小加倍: static String[] upSizeArr( String[] fullArr ) { int size = fullArr.length; String[] newSizeArr = new String[(2 * size)];

首先,为了便于理解,这里是我昨天的帖子:

所以我得到了这个NullPointerException,我现在相信它是在我试图找到字符串数组中第一个重复项的索引之前发生的。在搜索第一个副本的索引之前,我使用以下方法将字符串数组的大小加倍:

static String[] upSizeArr( String[] fullArr )
{

    int size = fullArr.length; 
    String[] newSizeArr = new String[(2 * size)]; 
    for (int a = 0; a < size; a++) {
        newSizeArr[a] = fullArr[a];
    }
    return newSizeArr;
}
使用upSizeArr方法是否有可能解决此问题?我更希望解决方案是基本的,只使用阵列而不使用其他数据结构。我是编程新手,我真的在努力掌握基本原理……大约一周左右的时间里,我一直在寻找解决此NullPointException的方法

以下是完整的代码:

import java.io.*;
import java.util.*;
public class Practice
{
    static final int CAPACITY = 10;
    static final int NOT_FOUND = -1;
    public static void main (String[] args) throws Exception
    {
        if (args.length < 1 )
        {
            System.out.println("\nusage: C:\\> java Practice <words filename>\n\n"); // i.e. C:\> java Lab2 10Kints.txt 172822words.txt
            System.exit(0);
        }


    String[] wordList = new String[CAPACITY];
    int wordCount = 0;
    BufferedReader wordFile = new BufferedReader( new FileReader(args[0]) );

    while ( wordFile.ready() ) // i.e. while there is another line (word) in the file
    {   if ( wordCount == wordList.length ) 
            wordList = upSizeArr( wordList );
        wordList[wordCount++] = wordFile.readLine();
    } //END WHILE wordFile
    wordFile.close(); 
    System.out.format( "%s loaded into word array. size=%d, count=%d\n",args[0],wordList.length,wordCount );
    int dupeIndex = indexOfFirstDupe( wordList, wordCount );
    if ( dupeIndex == NOT_FOUND )
        System.out.format("No duplicate values found in wordList\n");
    else
        System.out.format("First duplicate value in wordList found at index %d\n",dupeIndex);

} // END OF MAIN

// TWO METHODS 

static String[] upSizeArr( String[] fullArr )
{

    int size = fullArr.length; //find the length of the arrays
    String[] newSizeArr = new String[(2 * size)]; // creates new array, doubled in size
    for (int a = 0; a < size; a++) {
        newSizeArr[a] = fullArr[a];
    }
    return newSizeArr;

}
static int indexOfFirstDupe( String[] arr, int count )
{       
    Arrays.sort(arr);
    int size = arr.length;
    int index = NOT_FOUND;

    for (int x = 0; x < size; x++) {
        for (int y = x + 1; y < size; y++) {
            if (arr[x].equals(arr[y])) {
                index = x;
                break;
            }
        }
    }
    return index;
    }
} // END OF PROGRAM
import java.io.*;
导入java.util.*;
公共课堂实践
{
静态最终int容量=10;
未找到静态最终整数=-1;
公共静态void main(字符串[]args)引发异常
{
如果(参数长度<1)
{
System.out.println(“\n用法:C:\\>java Practice\n\n”);//即C:\>java Lab2 10Kints.txt 172822 words.txt
系统出口(0);
}
字符串[]字列表=新字符串[容量];
int字数=0;
BufferedReader wordFile=new BufferedReader(new FileReader(args[0]));
while(wordFile.ready())//即文件中有另一行(word)时
{if(wordCount==wordList.length)
wordList=upSizeArr(wordList);
wordList[wordCount++]=wordFile.readLine();
}//在wordFile时结束
close();
System.out.format(“%s加载到word数组中。大小=%d,计数=%d\n”,参数[0],字列表。长度,字计数);
int dupeIndex=indexOfFirstDupe(字表,字数);
如果(dupeIndex==未找到)
System.out.format(“在单词列表中找不到重复的值\n”);
其他的
System.out.format(“在索引%d\n处找到的单词列表中的第一个重复值”,dupeIndex);
}//主管道末端
//两种方法
静态字符串[]向上对齐(字符串[]向上对齐)
{
int size=fullArr.length;//查找数组的长度
String[]newsizerr=新字符串[(2*大小)];//创建新数组,大小加倍
对于(int a=0;a

另外,用作参数的文件是一个字符串的txt文件。

我不确定这是否是您的问题的原因,但它非常可疑

while ( wordFile.ready() ) {
    //...
}
这不是您应该如何读取文件。相反,您应该检查
readLine
的返回结果,当它到达文件末尾时,将返回
null

也许更像

try (BufferedReader wordFile = new BufferedReader(new FileReader(args[1]))) {
    String[] wordList = new String[CAPACITY];

    String text = null;
    while ((text = wordFile.readLine()) != null) {
        if (wordCount == wordList.length) {
            wordList = upSizeArr(wordList);
        }
        wordList[wordCount++] = text;
    }
} catch (IOException ex) {
    ex.printStackTrace();
}
您的代码还可能导致文件资源处于打开状态。上面的示例使用了
try with resources
语句来确保它正确关闭,而不管操作是否成功

请查看以了解更多详细信息

除非是特殊要求,否则我还建议使用
ArrayList
或像这样滚动您自己的解决方案

也许可以看看更多的细节

从可运行示例更新。。。 在玩了一个没有可运行代码示例的游戏之后,当
upSizeArr
创建一个新数组时,它会将新元素默认为
null
,这是意料之中的,我很惊讶
Arrays.sort
无法处理这个问题

“A”解决方案是用不同的非默认值填充未使用的空间

static String[] upSizeArr(String[] fullArr) {

    int size = fullArr.length; //find the length of the arrays
    String[] newSizeArr = new String[(2 * size)]; // creates new array, doubled in size
    for (int a = 0; a < size; a++) {
        newSizeArr[a] = fullArr[a];
    }
    for (int a = size; a < newSizeArr.length; a++) {
        newSizeArr[a] = "";
    }
    return newSizeArr;

}
在我的测试中,输出

words.txt loaded into word array. size=160, count=99
words.txt loaded into word array. size=99, count=99
No duplicate values found in wordList

我不确定这是否是你的问题的原因,但这是非常可疑的

while ( wordFile.ready() ) {
    //...
}
这不是您应该如何读取文件。相反,您应该检查
readLine
的返回结果,当它到达文件末尾时,将返回
null

也许更像

try (BufferedReader wordFile = new BufferedReader(new FileReader(args[1]))) {
    String[] wordList = new String[CAPACITY];

    String text = null;
    while ((text = wordFile.readLine()) != null) {
        if (wordCount == wordList.length) {
            wordList = upSizeArr(wordList);
        }
        wordList[wordCount++] = text;
    }
} catch (IOException ex) {
    ex.printStackTrace();
}
您的代码还可能导致文件资源处于打开状态。上面的示例使用了
try with resources
语句来确保它正确关闭,而不管操作是否成功

请查看以了解更多详细信息

除非是特殊要求,否则我还建议使用
ArrayList
或像这样滚动您自己的解决方案

也许可以看看更多的细节

从可运行示例更新。。。 在玩了一个没有可运行代码示例的游戏之后,当
upSizeArr
创建一个新数组时,它会将新元素默认为
null
,这是意料之中的,我很惊讶
Arrays.sort
无法处理这个问题

“A”解决方案是用不同的非默认值填充未使用的空间

static String[] upSizeArr(String[] fullArr) {

    int size = fullArr.length; //find the length of the arrays
    String[] newSizeArr = new String[(2 * size)]; // creates new array, doubled in size
    for (int a = 0; a < size; a++) {
        newSizeArr[a] = fullArr[a];
    }
    for (int a = size; a < newSizeArr.length; a++) {
        newSizeArr[a] = "";
    }
    return newSizeArr;

}
在我的测试中,输出

words.txt loaded into word array. size=160, count=99
words.txt loaded into word array. size=99, count=99
No duplicate values found in wordList

我看不到您提供的任何代码会取消对null的引用。你能提供更多的上下文/代码吗?异常具体在哪里抛出?在您链接到的原始帖子中,异常是由于包含
null
s的数组的一半(调整大小后的“新”一半)导致的,该数组
数组。sort
将在尝试比较
null
s时引发异常。这可以通过创建一个处理
null
的比较器来解决,如下所示:我建议
ArrayList
wordFile.ready()
不做你认为它做的事情,这行实际上抛出了异常?因此我对你的代码进行了一次非常快速的破解,没有尝试读取文件,它也不会生成NPE。我怀疑是
wordFile.ready()的组合