㈠ java數組里可以存NULL值嗎 取出時會報空指針嗎
可以存NULL值,單純取出來不用不會報NPE錯誤,但是如果你取出來用的話就會報NPE
㈡ java學習, 數組中為什麼會出現null
這里真的賦值了?str1[0]考慮沒有null值,就是從這開始的,二且涉及到大量string賦值操作建議你用stringbuilder或者stringbuffer(線程安全)
㈢ Java中如何判斷數組元素是否為空
那要看數組元素是干什麼了
比如是基礎類型,如int,long byte 等等 ,那肯定都不是空,默認為0
如果是Object,直接判斷就可以了,比如
Object[]objs=newObject[2];
if(objs[0]==null){
//這樣處理就好了
}
㈣ java字元串錄入數組後顯示null
原因在於你最後for循環輸出中i<length里的length取值有問題。你這段程序中,length取的是文件的大小,long length = file.length()返回的是當前文件的所有字元的數量。假如你的文件里的內容是:
第一行:a
第二行:bc
那麼此時file.length()是多少呢?其實它是'a'、' '、'b'、'c'、' '五個字元組成,所以此時length=5,然而你讀取文件的時候,你用的是readline(),所以會讀到兩行數據,而你的預期結果也應該是arr數組中有兩個字元串。
所有數據讀取完後,又由於你arr數組的長度取的是leagth也就是5,此時arr數組的arr[0]="a",arr[1]="bc",arr[2]=null,arr[3]=null,arr[4]=null。所以你最後循環輸出arr數組時,後面幾個值就是null。
綜上,該問題的根本原因在於arr數組長度的定義有誤。因為你提前並不能知道文件有多少行,所以你無法確認arr數組到底該設置多少,你就想用file.length()來定義,但file.length()往往是遠大於文件行數的。
解決這個問題,可以利用java里的集合List,List在定義的時候不需要聲明其長度,最大長度應該是Integer的最大值,使用List的子類ArrayList可以確保順序無誤,將代碼修改為:
㈤ java輸出的數組前面有NULL
import java.util.*;
public class StudentSystem_1 {
public static void main(String[] args) {
Scanner = new Scanner(System.in);
System.out.println("請輸入學生人數:");
int N_stu = .nextInt();
System.out.println("請輸入科目數:");
int N_cou = .nextInt();
String[] name = new String[N_stu];
String[] kemu = new String[N_cou];
int[][] course = new int[N_cou][N_cou];
int[] sum = new int[N_cou];
String[] str = new String[N_stu];
String[] str11 = new String[N_stu];
int[] avg = new int[N_stu];
for (int i = 0; i < kemu.length; i++) {// 循環填課程
System.out.println("請輸入第" + (i + 1) + "個課程名:");
kemu[i] = .next();
}
for (int i = 0; i < name.length; i++) {// 循環填姓名
System.out.println("請輸入第" + (i + 1) + "個學生的姓名:");
name[i] = .next();
// 加這個
str11[i] = "";
for (int j = 0; j < kemu.length; j++) {// 循環填成績
System.out.println("請輸入" + name[i] + "的" + kemu[j] + "成績:");
course[i][j] = .nextInt();
sum[i] += course[i][j];
str11[i] += course[i][j] + "\t";
}
avg[i] = sum[i] / kemu.length;
str[i] = name[i] + "\t" + str11[i] + sum[i] + "\t" + avg[i];
} // 判斷冒泡排序
for (int i = 0; i < sum.length - 1; i++) {
if (sum[i] < sum[i + 1]) {
String b = str[i];
str[i] = str[i + 1];
str[i + 1] = b;
}
} // 輸出!
System.out.print("姓名\t");// 輸出姓名
for (int i = 0; i < kemu.length; i++) {// 循環顯示課程
System.out.print(kemu[i] + "\t");
}
System.out.print("總分\t平均分");
System.out.print("\n");
for (int i = 0; i < name.length; i++) {// 循環輸出分數
System.out.println(str[i]);
}
}
}
㈥ java中如何判斷一個數組中的值全為null
inta=0;然後在for循環裡面判斷,如果file2[i]!=null的話,a=a+1;這樣的話,如果file2全為null的話,a就是0.直接判斷a的值就可以了
㈦ java為什麼數組一直是null
因為數組是引用類型,默認初始值是null
㈧ java中的數組元素不能等於null吧,會有異常的吧
不可以等於null的;如果等於了null就會報空指針異常;
如果判斷數組是否為NULL,就直接判斷 if(array == null){ }循環遍歷每個數組元素,然後判斷每個元素是不是NULL,前提是數組元素為對象,而不是基礎數據類型。
for(int i =0;i<array.length();i++){
if(array[i] == NULL){
}
}
當應用程序試圖在需要對象的地方使用 null 時,拋出該異常。這種情況包括:
1、 調用 null 對象的實例方法。
2、訪問或修改 null 對象的欄位。
3、將 null 作為一個數組,獲得其長度。
4、將 null 作為一個數組,訪問或修改其時間片。
5、將 null 作為 Throwable 值拋出。
㈨ java 中聲明了int數組沒有初始化時,int數組的默認值為什麼是null而不是0
int[]數組不是基本類型,是對象,所以默認為null
java裡面所有對象沒有new得時候,都為null,基本類型int才是默認為0
㈩ java怎麼判斷數組裡面是NULL
如果判斷數組是否為NULL,就直接判斷 if(array == null){ }
循環遍歷每個數組元素,然後判斷每個元素是不是NULL,前提是數組元素為對象,而不是基礎數據類型。
for(int i =0;i<array.length();i++){
if(array[i] == NULL){
}
}