Java基础day4
生活随笔
收集整理的這篇文章主要介紹了
Java基础day4
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Java基礎
- java基礎day4
- 1. idea安裝與使用
- 2. 數組
- 2.1定義
- 2.2 數組定義格式
- 2.3 數組動態初始化
- 2.4 數組元素訪問
- 2.5 內存分配
- 2.6 靜態初始化
- 2.7 數組操作的兩個常見小問題
- 2.8 數組遍歷
- 2.9 數組最值
java基礎day4
1. idea安裝與使用
常用快捷鍵:
快速生成語句快速生成main()方法:psvm,回車快速生成輸出語句:sout,回車內容輔助鍵Ctrl+Alt+space(內容提示,代碼補全等)快捷鍵:注釋:單行:選中代碼,Ctrl+/,再來一次,就是取消多行:選中代碼,Ctrl+Shift+/,再來一次,就是取消格式化Ctrl+Alt+L2. 數組
2.1定義
數組就是存儲數據長度固定的容器,存儲多個數據的數據類型要一致。
2.2 數組定義格式
1、數據類型【】 數組名
示例:
2、數據類型 數組名【】
示例:
2.3 數組動態初始化
數組動態初始化就是只給定數組的長度,由系統給出默認的初始化值
格式:
2.4 數組元素訪問
數組名[索引];//索引表示數組內元素的編號
public class day4 {public static void main(String[] args) {int[] arr = new int[3];System.out.println(arr);System.out.println(arr[0]);System.out.println(arr[1]);System.out.println(arr[2]);} }2.5 內存分配
棧內存和堆內存
2.6 靜態初始化
定義:在創建數組時,直接將元素確定
完整版格式
數據類型【】 數組名 = new 數據類型【】 {元素1,元素2,…};
簡化版格式
數據類型【】 數組名 = {元素1,元素2,…};
2.7 數組操作的兩個常見小問題
1、索引越界異常
public class day4 {public static void main(String[] args) {int[] arr = new int[3];System.out.println(arr[3]);} } 異常提示:ArrayIndexOutOfBoundsException2、空指針異常
public class day4 {public static void main(String[] args) {int[] arr = new int[3];arr = null;System.out.println(arr[0]);} } 異常提示:NullPointerException2.8 數組遍歷
public class day4{public static void main(String[] args) {int[] arr = {11, 22, 33, 44};System.out.println(arr[0]);System.out.println(arr[1]);System.out.println(arr[2]);System.out.println(arr[3]);//for 循環輸出for(int i = 0; i<arr.length; i++){System.out.println(arr[i]);}} }2.9 數組最值
示例:
public class day4{public static void main(String[] args) {int[] arr = {11, 22, 33, 100};int max = arr[0];for(int i = 1; i<arr.length; i++){if(max < arr[i]){max = arr[i];}}System.out.println("max:" + max);} }總結
以上是生活随笔為你收集整理的Java基础day4的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: XSSFWorkbook与HSSFWor
- 下一篇: Java中String类的concat方