选择性输出数组java_java基础-数组

数组是一个固定长度的,包含了相同类型数据的 容器

1.创建数组

int[] a:声明了一个数组变量。

[]:表示该变量是一个数组。

int:表示数组里的每一个元素都是一个整数。

a:是变量名。

但是,仅仅是这一句声明,不会创建数组

有时候也会写成int a[];没有任何区别

创建数组的时候,要指明数组的长度。

new int[5]

public classHelloWorld {public static voidmain(String[] args) {int[] a;//声明一个引用

a = new int[5];//创建一个长度是5的数组,并且使用引用a指向该数组

a[0] = 1; //下标0,代表数组里的第一个数

a[1] = 2;

a[2] = 3;

a[3] = 4;

a[4] = 5;int[] b = new int[5]; //声明的同时,指向一个数组

System.out.println("数组b的长度:" + b.length); //打印数组的长度

}

}

2.for循环,遍历数组找出最小的一个值

public classHelloWorld {public static voidmain(String[] args) {int[] a;//声明一个引用

a = new int[5];//创建一个长度是5的数组,并且使用引用a指向该数组

a[0] = (int) (Math.random() * 100); //下标0,代表数组里的第一个数

a[1] = (int) (Math.random() * 100);

a[2] = (int) (Math.random() * 100);

a[3] = (int) (Math.random() * 100);

a[4] = (int) (Math.random() * 100);

System.out.println("数组中的各个随机数是:");for (int i = 0; i < a.length; i++)

System.out.println(a[i]);int c = a[0];for (int i = 0; i < a.length; i++) {

System.out.println("数组中的a[" + i + "]随机数是:" +a[i]);if (c >a[i]) {

c=a[i];

}

}

System.out.println("数组中最小的一个值: " +c);

}

}

3.分配空间,赋值

public classHelloWorld {public static voidmain(String[] args) {//写法一: 分配空间同时赋值

int[] a = new int[] { 100, 102, 444, 836, 3236};//写法二: 省略了new int[],效果一样

int[] b = { 100, 102, 444, 836, 3236};//写法三:分配了长度是5的数组,但是没有赋值

int[] c = new int[5];//没有赋值,那么就会使用默认值//作为int类型的数组,默认值是0

System.out.println(c[0]);//进行赋值

c[0] = 100;

c[1] = 102;

c[2] = 444;

c[3] = 836;

c[4] = 3236;

}

}

4.数组反转

public classHelloWorld {public static voidmain(String[] args) {//声明并赋值数组

int[] a = new int[] { 11, 2, 44, 577, 54, 4};

printArray(a);

reverseArray1(a);

printArray(a);

reverseArray2(a);

printArray(a);

}//使用for实现对数组的反转

public static void reverseArray1(int[] a) {for (int i = 0; i < a.length / 2; i++) { //注意i不可以等于a.length

swap(a, i, a.length - 1 -i);

}

}//交换方法--提高代码重用性

public static void swap(int[] a, int x, inty) {int temp =a[x];

a[x]=a[y];

a[y]=temp;

}//遍历打印数组

public static void printArray(int[] a) {for (int i = 0; i < a.length; i++) {

System.out.print(a[i]+ " ");

}

System.out.println("\n-----------"); //可见java里也是可以使用 \n 符的,用于换行

}//使用while反转数组元素

public static void reverseArray2(int[] a) {int i = 0;while (i < a.length / 2) {

swap(a, i, a.length- 1 -i);

i++; //注意i不可以写在上面

}

}

}

5.排序

1.选择法排序

把第一位和其他所有的进行比较,只要比第一位小的,就换到第一个位置来比较完后,第一位就是最小的然后再从第二位和剩余的其他所有进行比较,只要比第二位小,就换到第二个位置来比较完后,第二位就是第二小的,以此类推。

2.冒泡法排序

第一步:从第一位开始,把相邻两位进行比较 如果发现前面的比后面的大,就把大的数据交换在后面,循环比较完毕后,最后一位就是最大的 第二步: 再来一次,只不过不用比较最后一位 以此类推

public classHelloWorld {public static voidmain(String[] args) {//TODO Auto-generated method stub

int[] a = { 9, 4, 7, 1, 3, 8, 2, 5, 6};

HelloWorld sort= newHelloWorld();

sort.sort1(a);

System.out.println();

sort.sort2(a);

}//选择排序

void sort1(int[] a) {intj;int i = 0;for (; i < a.length - 1; i++) {for (j = i + 1; j < a.length; j++) {if (a[i]

a[i]=a[j];

a[j]=temp;

}

}

}for (int d = 0; d < a.length; d++) {

System.out.print(a[d]+ " ");

}

}//冒泡排序

void sort2(int[] a) {for (int i = 0; i < a.length; i++) {for (int j = 0; j < a.length - i - 2; j++) {if (a[j + 1] >a[j]) {int temp =a[j];

a[j]=a[i];

a[i]=temp;

}

}

}for (int d = 0; d < a.length; d++) {

System.out.print(a[d]+ " ");

}

}

}

6.增强型for循环

增强型for循环只能用来取值,却不能用来修改数组里的值

public classHelloWorld {public static voidmain(String[] args) {int values[] = new int[] { 18, 62, 68, 82, 65, 9};//常规遍历

for (int i = 0; i < values.length; i++) {int each =values[i];

System.out.print(each);

System.out.print("\t");

}

System.out.println();//增强型for循环遍历

for (inteach : values) {

System.out.print(each);

System.out.print("\t");

}

System.out.println();int c = values[0];for (intb : values) {

System.out.print(b);

System.out.print("\t");if (b >c) {

c=b;

}

}

System.out.println("最大" +c);

}

}

7.复制数组

数组的长度是不可变的,一旦分配好空间,是多长,就多长,不能增加也不能减少。

把一个数组的值,复制到另一个数组中

System.arraycopy(src, srcPos, dest, destPos, length)

src: 源数组

srcPos: 从源数组复制数据的起始位置

dest: 目标数组

destPos: 复制到目标数组的起始位置

length: 复制的长度

public classHelloWorld {public static voidmain(String[] args) {int a[] = new int[] { 18, 62, 68, 82, 65, 9};int b[] = new int[3];//分配了长度是3的空间,但是没有赋值//通过数组赋值把,a数组的前3位赋值到b数组//方法一: for循环

for (int i = 0; i < b.length; i++) {

b[i]=a[i];

}//把内容打印出来

for (int i = 0; i < b.length; i++) {

System.out.print(b[i]+ " ");

}

System.out.println();//方法二: System.arraycopy(src, srcPos, dest, destPos, length)//src: 源数组//srcPos: 从源数组复制数据的起始位置//dest: 目标数组//destPos: 复制到目标数组的启始位置//length: 复制的长度

System.arraycopy(a, 0, b, 0, 3);//把内容打印出来

for (int i = 0; i < b.length; i++) {

System.out.print(b[i]+ " ");

}

}

}

8.二维数组

//这是一个一维数组, 里面的每一个元素,都是一个基本类型int

int a[] =new int[]{1,2,3,4,5};

//这是一个二维数组,里面的每一个元素,都是一个一维数组;所以二维数组又叫数组的数组。

int b[][] = new int[][]{

{1,2,3},

{4,5,6},

{7,8,9}

};

public classHelloWorld {public static voidmain(String[] args) {//初始化二维数组,

int[][] a = new int[2][3]; //有两个一维数组,每个一维数组的长度是3

a[1][2] = 5; //可以直接访问一维数组,因为已经分配了空间//只分配了二维数组

int[][] b = new int[2][]; //有两个一维数组,每个一维数组的长度暂未分配

b[0] = new int[3]; //必须事先分配长度,才可以访问

b[0][2] = 5;//指定内容的同时,分配空间

int[][] c = new int[][] { { 1, 2, 4 }, { 4, 5 }, { 6, 7, 8, 9} };

}

}

定义一个5X5的二维数组。 然后使用随机数填充该二维数组。 找出这个二维数组里,最大的那个值,并打印出其二维坐标。

public classHelloWorld {public static voidmain(String[] args) {int[][] array = new int[5][5];//第一轮嵌套循环,给数组赋值

for (int i = 0; i < array.length; i++) {for (int j = 0; j < array.length; j++) {

array[i][j]= (int) (Math.random() * 100);

}

}

System.out.println("二维数组的元素为:");//遍历三维数组元素并打印//输出看看

for (int i = 0; i < array.length; i++) {for (int i1 = 0; i1 < array[i].length; i1++) {

System.out.print(array[i][i1]+ "\t");

}

System.out.println();//换行

}//第二轮嵌套循环,找出数组中最大值

int temp = array[0][0];for (int i = 0; i < array.length; i++) {for (int j = 0; j < array[i].length; j++) {if (temp

temp=array[i][j];

}

}

}

System.out.println("最大值为:" +temp);for (int i = 0; i < array.length; i++) {for (int j = 0; j < array[i].length; j++) {if (array[i][j] ==temp) {

System.out.println("最大值的坐标为:第" + (i + 1) + "行,第" + (j + 1) + "列");

}

}

}

}

}

拓展:定义一个5X5X5的三维数组。 然后使用随机数填充该三维数组。 找出这个三维数组里,最大的那个值,并打印出其三维坐标。

public classArr {public static voidmain(String[] args) {int[][][] array = new int[5][5][5];//第一轮嵌套循环,给数组赋值

for (int i = 0; i < array.length; i++) {for (int j = 0; j < array.length; j++) {for (int k = 0; k < array.length; k++) {

array[i][j][k]= (int) (Math.random() * 100);

}

}

}

System.out.println("三维数组的元素为:");//遍历三维数组元素并打印//输出看看

for (int i = 0; i < array.length; i++) {for (int i1 = 0; i1 < array[i].length; i1++) {for (int i2 = 0; i2 < array[i][i1].length; i2++) {

System.out.print(array[i][i1][i2]+ "\t");

}

System.out.println();//二维换行

}

System.out.println();//三维换行

}//第二轮嵌套循环,找出数组中最大值

int temp = array[0][0][0];for (int i = 0; i < array.length; i++) {for (int j = 0; j < array[i].length; j++) {for (int k = 0; k < array[i][j].length; k++) {if (temp

temp=array[i][j][k];

}

}

}

}

System.out.println("最大值为:" +temp);for (int i = 0; i < array.length; i++) {for (int j = 0; j < array[i].length; j++) {for (int k = 0; k < array[i][j].length; k++) {if (array[i][j][k] ==temp) {

System.out.println("最大值的坐标为:第" + (i + 1) + "个二维数组,第" + (j + 1) + "行,第" + (k + 1) + "列");

}

}

}

}

}

}

9.Arrays

Arrays是针对数组的工具类,可以进行 排序,查找,复制填充等功能。 大大提高了开发人员的工作效率。

copyOfRange

数组复制

toString()

转换为字符串

sort

排序

binarySearch

搜索

equals

判断是否相同

fill

填充

数组复制

System.arraycopy(dataType[] srcArray,int srcIndex,int destArray,int destIndex,int length)

srcArray 表示原数组,srcIndex 表示源数组中的起始索引,destArray 表示目标数组,destIndex 表示目标数组中的起始索引,length 表示要复制的数组长度。

public classHelloWorld {public static voidmain(String[] args) {//合并数组

int[] a = new int[(int) (Math.random() * 5 + 5)];int[] b = new int[(int) (Math.random() * 5 + 5)];int[] c = new int[a.length +b.length];int[] d = new int[a.length +b.length];//给a数组赋值

System.out.println("a数组内容:");for (int i = 0; i < a.length; i++) {

a[i]= (int) (Math.random() * 10);

System.out.print(a[i]+ " ");

}//给b数组赋值

System.out.println(" ");

System.out.println("b数组内容:");for (int i = 0; i < b.length; i++) {

b[i]= (int) (Math.random() * 10);

System.out.print(b[i]+ " ");

}//第一种:for循环将a、b数组赋值到c

for (int j = 0; j < a.length; j++) {

c[j]=a[j];

}int flag = 0;for (int k = a.length; k < a.length + b.length; k++) {

c[k]=b[flag];

flag++;

}

System.out.println(" ");

System.out.println("c数组内容:");for (int i = 0; i < c.length; i++) {

System.out.print(c[i]+ " ");

}//第二种:通过System.arraycopy将a、b数组赋值到d;//System.arraycopy(src, srcPos, dest, destPos, length)

System.arraycopy(a, 0, d, 0, a.length);

System.arraycopy(b,0, d, a.length, b.length);

System.out.println(" ");

System.out.println("d数组内容:");//循环打印d

for (intd1 : d) {

System.out.print(d1+ " ");

}

}

}

Arrays.copyOf(dataType[] srcArray,int length);

srcArray 表示要进行复制的数组,length 表示复制后的新数组的长度。

Arrays.copyOfRange(dataType[] srcArray,int startIndex,int endIndex)

srcArray 表示原数组,startIndex 表示开始复制的起始索引,endIndex 表示终止索引。

与使用System.arraycopy进行数组复制类似的, Arrays提供了一个copyOfRange方法进行数组复制。

不同的是System.arraycopy,需要事先准备好目标数组,并分配长度。 copyOfRange 只需要源数组就就可以了,通过返回值,就能够得到目标数组了。

除此之外,需要注意的是 copyOfRange 的第3个参数,表示源数组的结束位置,是取不到的。

importjava.util.Arrays;public classHelloWorld {public static voidmain(String[] args) {int a[] = new int[] { 18, 62, 68, 82, 65, 9};//copyOfRange(int[] original, int from, int to)//第一个参数表示源数组//第二个参数表示开始位置(取得到)//第三个参数表示结束位置(取不到)

int[] b = Arrays.copyOfRange(a, 0, 3);for (int i = 0; i < b.length; i++) {

System.out.print(b[i]+ " ");

}

}

}

转换为字符串

Arrays.toString(Object[] array)

功能:返回数组的字符串形式

importjava.util.Arrays;public classHelloWorld {public static voidmain(String[] args) {int a[] = new int[] { 18, 62, 68, 82, 65, 9};

String content=Arrays.toString(a);

System.out.println(content);

}

}

Arrays.deepToString(Object[][] arrays)

功能:返回多维数组的字符串形式

importjava.util.Arrays;public classHelloWorld {public static voidmain(String[] args) {int[][] nums = { { 1, 2 }, { 3, 4} };//返回多维数组的字符串形式,Arrays.deepToString(Object[][] arrays)

String content =Arrays.deepToString(nums);

System.out.println(content);

}

}

排序

Arrays.sort(Object[] array)

功能:对数组按照升序排序

importjava.util.Arrays;public classHelloWorld {public static voidmain(String[] args) {int a[] = new int[] { 18, 62, 68, 82, 65, 9};

System.out.println("排序之前 :");

System.out.println(Arrays.toString(a));

Arrays.sort(a);

System.out.println("排序之后:");

System.out.println(Arrays.toString(a));

}

}

Arrays.sort(Object[] array, int from, int to)

// 第一个参数表示源数组

// 第二个参数表示开始位置(取得到)

// 第三个参数表示结束位置(取不到)

功能:部分排序,对数组元素指定范围进行排序(排序范围是从元素下标为from,到下标为to-1的元素进行排序)

importjava.util.Arrays;public classHelloWorld {public static voidmain(String[] args) {int a[] = new int[] { 18, 62, 68, 82, 65, 9};

System.out.println("排序之前 :");

System.out.println(Arrays.toString(a));

Arrays.sort(a,0,3);

System.out.println("排序之后:");

System.out.println(Arrays.toString(a));

}

}

搜索

binarySearch(Object[] a,Object key);

功能:查询元素出现的位置,返回数组下标,否则返回-1。

需要注意的是,使用binarySearch进行查找之前,必须使用sort进行排序

如果数组中有多个相同的元素,查找结果是不确定的

importjava.util.Arrays;public classHelloWorld {public static voidmain(String[] args) {int a[] = new int[] { 18, 62, 68, 82, 65, 9};

Arrays.sort(a);

System.out.println(Arrays.toString(a));// 使用binarySearch之前,必须先使用sort进行排序

System.out.println("数字 62出现的位置:" + Arrays.binarySearch(a, 62));

}

}

binarySearch(Object[] a,int fromIndex,int toIndex,Object key);

a 表示要进行查找的数组,fromIndex 指定范围的开始处索引(包含开始处),toIndex 指定范围的结束处索引(不包含结束处),key 表示要搜索的元素。

数组的比较

Arrays.equals(arrayA, arrayB);

功能:比较两个数组的内容是否一样

第二个数组的最后一个元素是8,和第一个数组不一样,所以比较结果是false

importjava.util.Arrays;public classHelloWorld {public static voidmain(String[] args) {int a[] = new int[] { 18, 62, 68, 82, 65, 9};int b[] = new int[] { 18, 62, 68, 82, 65, 8};

System.out.println(Arrays.equals(a, b));

}

}

deepEquals(Object[] array,Object[] array)

功能:比较的是对象中内容是否相等、比较两个多维数组的内容是否一样

importjava.util.Arrays;public classHelloWorld {public static voidmain(String[] args) {int a[] = new int[] { 18, 62, 68, 82, 65, 9};int b[] = new int[] { 18, 62, 68, 82, 65, 8};int c[][] = new int[][] { { 18, 62, 68, 82, 65, 9 }, { 18, 62, 68, 82, 65, 9} };int d[][] = new int[][] { { 18, 62, 68, 82, 65, 9 }, { 18, 62, 68, 82, 65, 9} };

String[][] e= { { "G", "a" }, { "H", "u", "n" }, { "j", "e"} };

String[][] f= { { "G", "a" }, { "H", "u", "n" }, { "j", "e"} };

System.out.println(Arrays.equals(a, b));//false//System.out.println(Arrays.deepEquals(a, b));//会报错

System.out.println(Arrays.equals(c, d));//false

System.out.println(Arrays.deepEquals(c, d));//true

System.out.println(Arrays.equals(e, f));//false

System.out.println(Arrays.deepEquals(e, f));//true

}

}

填充

Arrays.fill(array,value);

array 表示数组,value 表示填充的值。只能使用同一个数值进行填充。

功能:可以为数组元素填充相同的值

importjava.util.Arrays;public classHelloWorld {public static voidmain(String[] args) {int a[] = new int[10];

Arrays.fill(a,5);

System.out.println(Arrays.toString(a));

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值