當前位置:首頁 » 編程語言 » java數組個數

java數組個數

發布時間: 2024-02-28 04:01:26

1. java統計數組中連續元素出現的個數

		int[]nums=newint[]{0,2,3,7,8,9,10,12};
intcount=0;
for(inti=0;i<nums.length-1;i++){
if(nums[i]==nums[i+1]-1){
System.err.println("符合《《"+nums[i]);
count++;
}elseif(i>0){
if(nums[i]-1==nums[i-1]){
System.err.println("符合《《"+nums[i]);
count++;
}
}
}
System.err.println(count);
//不知道你怎麼算的5,我理解的實現算出來是6啊

2. java中怎麼獲得數組中的元素的個數

java中知道數組中實際的元素個數的方法:
定義一個變數保存數組中的元素個數,然後對數組進行遍歷,當數組元素不為空時,變數的值+1,否則不改變。
代碼為:

1
2
3
4
5
6

int n = 0; //保存元素個數的變數
for(int i = 0; i < array.length; i++)
{
if(null != array[i]) n++;
}
System.out.println(n);

3. java問題:請問如何表達數組A的元素個數啊

查看數組A的元素個數,直接調用 A.length 就可以了

也就是說 數組A有length屬性 ,但沒有length()方法 也沒有size屬性 .

參考代碼如下

publicclassMyDemo{

publicstaticvoidmain(String[]args){
//查看數組的長度,使用數組.length屬性即可
int[]ary={1,2,3};
System.out.println(ary.length);

//查看字元串的長度,使用字元串.length()方法即可
Stringstr="AB";
System.out.println(str.length());

//查看集合的長度,使用集合.size()方法即可
ArrayList<String>list=newArrayList<String>();
list.add("蘋果");
System.out.println(list.size());
}
}

輸出結果

3
2
1
熱點內容
字元集資料庫 發布:2025-09-08 23:47:35 瀏覽:994
pythonemail解析 發布:2025-09-08 23:37:02 瀏覽:311
android背景高斯模糊 發布:2025-09-08 23:36:03 瀏覽:944
如何解除淘寶的銀行卡快捷支付密碼 發布:2025-09-08 23:35:23 瀏覽:826
手機復制訪問 發布:2025-09-08 23:33:41 瀏覽:226
壓縮a和t 發布:2025-09-08 23:15:12 瀏覽:475
紅米4音樂文件夾是哪個 發布:2025-09-08 23:15:02 瀏覽:35
鐵皮文件櫃密碼盤鎖如何拆解 發布:2025-09-08 23:05:49 瀏覽:988
安卓哪個平板吃雞好 發布:2025-09-08 22:58:53 瀏覽:618
那隻貓編程 發布:2025-09-08 22:54:23 瀏覽:731