java.数组

mac2022-06-30  76

数组

别人的讲解

数组的概念

数组是一组数据类型相同的数据的组合,将这些数据统一的管理起来 数组是一个引用数据类型,数组内存的类型可以是基本类型,也可以是引用类型

数组的定义(声明)

数据类型[] 数组名字: int[] x; char[] y; boolean[] z; String[] m; 其他的定义方式: int x[]; int []x;

数组的赋值(初始化)

静态初始化 有长度 有元素 int[] array = new int[]{10,23,30,40,50}; int[] array = {10,20,30,40,50}; 动态初始化 有长度 没有元素(不是真的没有 是保存默认值) int[] array = new int[5]; 整数默认值--0 浮点数默认值--0.0 字符类型默认值--char对应0的字解码 比如 97对应a 65对应A 48对应‘0’ 布尔型默认值--false 引用数据默认值--null

访问元素

通过元素在数组的位置index(索引/下标)来访问 索引是有取值范围的【从0开始-数组长度-1】 如果数组的索引超出了上述范围 会出现一个运行异常 ArrayIndexOutOfBoundsException (数组索引超出边界)

数组元素的遍历(轮询)

通过循环的方式访问数组的每一个元素 JDK1.5版本之后 新的特性 增强for循环: forEach for(自定义的变量(用于接收数组内的每一个元素): 遍历的数组Array){ } 正常的for 有三个必要条件 index索引 找到某一个位置 可以通过index直接访问数组的某一个位置 存值 取值都可以 增强的for 有两个必要条件 用来取值的变量 用来的遍历的数组 没有index索引 不能存值 只能取值 没有index索引 找不到元素到底是哪一个

练习

用数组来存储1-100之间的奇数

public class Test0 { public static void main(String[] args){ //静态初始化a、b数组 int[] a = new int[]{1,2,3,4}; int[] b = new int[]{5,6,7,8}; //堆内存的数组空间长度一旦确定 不能再次发生改变。那我们定义一个动态数组器调换元素作用 int[] x = new int[4]; //for循环将a数组中的元素赋值到x数组中,现在x{1,2,3,4} for (int i=0;i<a.length;i++){ x[i] = a[i]; } //for循环将b数组中的元素赋值到a数组中,现在a{5,6,7,8} for (int i=0;i<a.length;i++){ a[i] = b[i]; } //for循环将x数组中的元素赋值到b数组中,现在b{1,2,3,4} for (int i=0;i<a.length;i++){ b[i] = x[i]; } //while循环遍历替换后的a和b中的元素 int z = 0; while(z<a.length){ System.out.println(a[z]); System.out.println(b[z]); z++; } } }

a{1,2,3,4} b{5,6,7,8}内的元素位置对应互换

public class Test1 { public static void main(String[] args) { //将数组a{1、2、3、4} b{5、6、7、8}内的元素位置对应互换 //1、每次循环将两个数组中对应位置的元素互换(循环对此,受长度限制) int[] a = {1,2,3,4}; int[] b = {5,6,7,8}; for(int i = 0;i<a.length;i++) { int x = a[i]; a[i] = b[i]; b[i] = x; System.out.println(a[i]); System.out.println(b[i]); } //2、直接交换数组a和b数组的引用地址(不用循环,不受长度限制) int [] y = a; a = b; b = y; for(int z : a) { System.out.println(z); } for(int z : b) { System.out.println(z); } } }

a{1,2,3,4,5,6}内的元素位置逆向调换

public class Test2 { public static void main(String[] args) { //静态生成一个数组 int[] a = new int[]{1,2,3,4,5,6,7,7,9}; //通过第三个int元素介入,每次循环从外向内进行元素互换 for(int i=0;i<a.length/2;i++) { int x = a[i]; a[i] = a[(a.length-1)-i]; a[(a.length-1)-i] = x; } for(int y:a) { System.out.println(y); } } }

a{1,2,3,4,5,6}计算数组所有元素的平均值

public class Test3 { public static void main(String[] args){ //静态定义一组数据 int[] a = new int[]{1,2,3,4,5}; //定义一个数值为0的int类型变量,用于接收a数组内的元素进行相加 int sum = 0; //while遍历数组a中的元素,进行相加 int i = 0; while(i<a.length){ sum = sum+a[i]; i++; } //打印出平均值 System.out.println(sum/a.length); } }

{1,3,5,7,9,0,2,4,6,8}找出最大值和最小值

public class Test4 { public static void main(String[] args){ /* * 1、定义一个值为0的int数据类型对象,用来接收a数组的元素 * 2、遍历a数组的元素,没次赋值给int对象 * 3、下一次遍历是判断当前int对象和上一次遍历被赋值的int对象谁更大, * 如果当前遍历对象更大则进行赋值,更小则进行下一次遍历。 * 4、最小值思路反之 */ //静态定义一数组 int[] a = new int[]{1,3,5,7,9,0,2,4,6,8}; int MaxNum = 0; for(int i = 0;i<a.length;i++){ if(a[i]>MaxNum){ MaxNum = a[i]; } } int MinNum = 0; for(int x = 0;x<a.length;x++){ if(a[x]<MinNum){ MinNum = a[x]; } } System.out.println("a数组中的最大值是:"+MaxNum); System.out.println("a数组中的最小值是:"+MinNum); } }

a{1,2,3} b{4,5} 合并两个数组

public class Test5 { public static void main(String[] args) { //创建两个数组 int[] a = {1,2,3}; int[] b = {4,5}; //创建一个新数组向内添加内容 int[] NewArray = new int[a.length+b.length]; //将a数组的元素添加进NewArray for(int i = 0;i<a.length;i++) { NewArray[i] = a[i]; } //将b数组的元素添加进NewArray for(int i = 0;i<b.length;i++) { NewArray[a.length+i] = b[i]; } //遍历输出NewArray for(int x:NewArray) { System.out.println(x); } } }

{5,4,2,3,1}升序排列

public class Test6 { public static void main(String[] main) { int[] a = { 5, 4, 2, 3, 1 }; for (int x = 1; x < 5; x++) {//重复执行升序循环 for (int i = 4; i >= x; i--) {//找出一个最小数进行升序替换 // 如果后面的数比前面的数大,就进行替换 if (a[i] < a[i-1]) { int change = a[i]; a[i] = a[i-1]; a[i-1] = change; } } } for (int x : a) { System.out.println(x); } } }

转载于:https://www.cnblogs.com/youngleesin/p/11572525.html

最新回复(0)