java数组
1.数组初始化的含义以及值:
数组初始化,即在内存开辟连续的n个空间。
初始化:
2.数组三要素:维度,数据类型,内存地址。
3.数组静态初始化与数组动态初始化:
4.数组存储图解:
4.打印数组所有元素
public class demo3_数组1 { public static void main(String[] args){ int[] arr= new int[]{11,22,33,44,55}; System.out.println(arr[0]); System.out.println(arr); System.out.println(arr[3]); for(int i=0;i<=4;i++) System.out.println(arr[i]); } }
数组初始化不允许动静结合。
5.数组倒序输出及遍历(traversal)
public class demo3_数组1 { public static void main(String[] args) { int[] arr1 = new int[]{11, 22, 33, 44, 55, 66}; convert(arr1); } public static void convert(int[] arr) { if (arr.length % 2 == 0) { for (int i = 0; i < (arr.length) / 2; i++) { int temp = arr[i]; arr[i] = arr[arr.length - 1 - i]; arr[arr.length - 1 - i] = temp; } for (int j = 0; j < arr.length; j++) { System.out.print(arr[j] + "\t"); } } else { for (int i = 0; i < (arr.length - 1) / 2; i++) { int temp2 = arr[i]; arr[i] = arr[arr.length - 1 - i]; arr[arr.length - 1 - i] = temp2; } for (int x = 0; x < arr.length; x++) { System.out.print(arr[x] + "\t"); } } } /* int max=arr1[0]; for(int i=1;i<arr1.length;i++){ if(max<arr1[i]){ max=arr1[i]; } } System.out.print(max); /* for(int i=0;i<arr1.length;i++) { System.out.print(arr1[i] + "\t"); } System.out.println(); int[] arr2={3,4,5}; /* Traversal(arr1); Traversal(arr2); */ /* public static void Traversal(int[] arr ){ for(int i=0;i<arr.length;i++){ System.out.print(arr[i]+"\t"); } } */ }
7.根据索引输出星期
import java.util.Scanner; public class weekBiao { public static void main(String[] args){ Scanner sc=new Scanner(System.in); System.out.println("请输入星期"); int week=sc.nextInt(); System.out.println("星期"+getWeek(week)); } public static char getWeek(int week ){ char [] getWeek= {' ','一','二','三','四','五','六','日',}; return(getWeek[week]); } }
8.查索引表
import java.util.Scanner; public class weekBiao { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入星期"); int week = sc.nextInt(); System.out.println("星期" + getWeek(week)); getIdex(); } public static char getWeek(int week) { char[] getWeek = {' ', '一', '二', '三', '四', '五', '六', '日',}; return (getWeek[week]); } //输入数字求索引 public static int getIdex(){ Scanner sc= new Scanner(System.in); System.out.println("请输入数字"); int value=sc.nextInt(); int [] arr=new int []{11,22,33,44,55}; for(int i=0;i<arr.length;i++){ if(arr[i]==value) { int index=i; System.out.println(index); } } return -1; } }
结果:
9.一二维数组的区别
10.二维数组内存图解
tips:java弹栈
11.java传值的原理