我有一个赋值,我应该创建一个对象,初始化一个字符串数组,使其具有“size”元素,并且使用的元素数量等于0。
我的问题是当我试图比较字符串以按字母顺序排列时。
int compare = storage[index].compareTo(value);
if (compare < 0)
那就是我得到nullpointerexception的运行时错误
这是我的完整代码。
班主
package assignment2;
public class Main {
public static void main(String[] args) {
OrderedStringList myList = new OrderedStringList(5);
System.out.println("adding 10, 5, & 7");
myList.Insert("10");
myList.Insert("5");
myList.Insert("7");
myList.Display();
System.out.println("Value 4 find = "+ myList.Find("4"));
System.out.println("Value 7 find = "+ myList.Find("7"));
System.out.println("Adding 24 & 3");
myList.Insert("24");
myList.Insert("3");
myList.Display();
System.out.println("myList size: "+ myList.Size());
if (!myList.Insert("12"))
System.out.println("Could not add 12, full");
System.out.println("Removing 10, adding 12.");
myList.Delete("10");
myList.Insert("12");
myList.Display();
}
}
class OrderedStringList
package assignment2;
public class OrderedStringList {
int length;
int numUsed;
String[] storage;
boolean ordered;
public OrderedStringList(int size){
length = size;
storage = new String[length];
numUsed = 0;
}
public boolean Insert(String value){
boolean result = false;
int index = 0;
if (numUsed < length) {
while (index <= numUsed) {
int compare = storage[index].compareTo(value);
if (compare < 0)
index++;
}
moveItemsDown(index);
storage[index] = value;
numUsed++;
result = true;
}
return result;
}
private void moveItemsDown(int start){
int index;
for (index = numUsed-1; index >=start; index--){
storage[index+1] = storage[index];
}
}
private void moveItemsUp(int start){
int index;
for (index = start; index < numUsed-1; index++){
storage[index] = storage[index+1];
}
}
public boolean Find(String value){
return (FindIndex(value) >= 0);
}
private int FindIndex(String value) {
int result = -1;
int index = 0;
boolean found = false;
while ((index < numUsed) && (!found)) {
found = (value.equals(storage[index]));
if (!found)
index++;
}
if (found)
result = index;
return result;
}
public boolean Delete(String value){
boolean result = false;
int location;
location = FindIndex(value);
if (location >= 0) {
moveItemsUp(location);
numUsed--;
result = true;
}
return result;
}
public void Display() {
int index;
System.out.println("list Contents: ");
for (index = 0; index < numUsed; index++) {
System.out.println(index+" "+storage[index]);
}
System.out.println("-------------");
System.out.println();
}
public void DisplayNoLF() {
int index;
System.out.println("list Contents: ");
for (index = 0; index < numUsed; index++) {
System.out.print(storage[index]+" ");
}
System.out.println("-------------");
System.out.println();
}
public int Size(){
return numUsed;
}
}
谢谢你们
答案 0 :(得分:1)
应该是
while(index < numUsed)
如果使用&lt; =,您将始终在空列表中访问索引0,该列表将为null。然后,当你尝试调用compareTo时,它会抛出一个NPE。
另外,如果null是要添加到列表中的合法值,则需要对compareTo调用进行空检查,并确定null是按字母顺序排在第一位还是最后一位。
答案 1 :(得分:1)
您可以使用Arrays#sort
维护订单,它已在库中提供。
public boolean Insert(String value){
boolean result = false;
if (numUsed < length) {
storage[index] = value;
numUsed++;
result = true;
Arrays.sort(storage);
}
return result;
}
答案 2 :(得分:0)
原因如下:
public boolean Insert(String value){
boolean result = false;
int index = 0;
if (numUsed < length) {
while (index <= numUsed) { // Here the first time numUsed = 0, index = 0, index <= numUsed;
int compare = storage[index].compareTo(value); // The first time, storage[0] == null; NullPointException is thrown
if (compare < 0)
index++;
}
moveItemsDown(index);
storage[index] = value;
numUsed++;
result = true;
}
return result;
}
也许将<=
更改为<
会这样做吗?
答案 3 :(得分:0)
第一次运行此行
int compare = storage[index].compareTo(value);
您正在比较storage[index]
哪个值为null
与value
not null or empty
使平稳运行 取代
while(index =< numUsed) {
int compare = storage[index].compareTo(value);
if(compare < 0)
index++;
}
与
while(index < numUsed) {
int compare = storage[index].compareTo(value);
if(compare < 0)
index++;
}