所以我已經頭腦風暴了一段時間了,即時在我的作業的最後一步和最後一步。我想我究竟做了,這只是我需要幫助擺脫這些空值:部分填充陣列空值
下面的代碼:
public static char[] readArray(char[] words){
char[] letters = new char[words.length];
letters = myInput(); //get input message
for (int i = 0 ; i < letters.length ; i++)
letters[i] = words[i] ; //store message to array of words
return words;
}
public static char[] myInput(){
// method to take message from user
String myMessage;
System.out.print("Input message: ");
Scanner myIn = new Scanner(System.in);
myMessage = myIn.nextLine();// Read a line of message
return myMessage.toCharArray();
}
public static void printOneInLine(char[] words){
//for every word, print them in a line
for (int i = 0 ; i < words.length ; i++){
if (words[i] == ' ') // words separated by space creates a new line
System.out.println();
else
System.out.print(words[i]); //print the word
}
}
測試用例:
input = hello world
output =
hello
world NUL NUL NUL NUL ...
我知道數組是部分填充並且因爲i < words.length
系統會嘗試顯示從0 - 256的數組值。任何建議將很樂意讚賞。 PS:新到Java
你應該改變你的問題。你試圖達到什麼目的?你嘗試了什麼,爲什麼它沒有工作? – 2014-10-26 17:34:50
爲什麼你用'char []'而不是'String'? – 2014-10-26 17:35:19
@cypressious我試圖擺脫NUL NUL NUL NUL。我試圖改變數組的大小,實際上是256.這是一種工作,但不是我真正想要的。 – 2014-10-26 17:42:43