Java에서이 프로그램을 가져 오는 방법을 알아내는 데 문제가 있습니다. 나는 클래스 단어 목록 했어야 해요 :정렬 된 배열에 문자열 삽입
public class WordList{
private int size; //number of words in array
private String array1[]; //array of words
private int capacity; // how big the array is supposed to be
을 그리고 우리는 두 개의 생성자가하는 거 야 : 먼저 하나
public WordList(int capacity){
this.array1 = new String[capacity]; //makes a new array of specified capacity
this.capacity = capacity; //sets the capacity
this.size = 0; //sets the size of array (i.e. # of words) to 0
}
두 번째 :
public WordList (String[] arrayOfWords){
this.capacity = 2 * arrayOfWords.length; //makes the capacity of array twice the # of words in input array
this.array1 = new String[capacity]; //makes a new array
this.size = arrayOfWords.length; //sets the # of words in array
for (int i = 0; i < arrayOfWords.length; i++){ //loops through array
this.insert(arrayOfWords[i]); //inserts the words (sorted into our array)
}
}
마지막을 삽입 메소드. 나는 주된 문제가 여기에 있다고 생각한다. 내 두 개의 생성자가 정확한지는 모르겠지만, 내가 잘못 여기서 뭔가 거기에 110 % 확신 :
단어의 이미 정렬 된 배열로 제공되는 단어를 삽입하고 목록을 유지하기로했다 기본적으로public void insert(String newword){
for (int i = 0; i < size; i++){
int l = newword.compareTo(array1[i]);
if (l > 0)
continue; // means that the word we're inserting is after
if (l < 0){
for (int j = size; j > i; j--){
array1[j] = array1[j-1]; //shifts all array elements over by one - starting at end of array to avoid over writing anything
}
array1[i] = newword;//inserts the word
}
if (l == 0)
return;//doesn't do anything if word is already in list
}
}
정렬. 프로그램이 충돌합니다. 무엇이 잘못 되었을지에 대한 아이디어가 있습니까?
'NullPointerException' ???????? – Biu