本文主要是介绍第九话-Java数组,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
数组声明创建
数组初始化
三种初始化
数组内存位置
数组4个基本特点
数组的使用
package com.xie.array;
public class Demo01 {
public static void main(String[] args) {
int[] arrays = {1,2,3,4,5};
for (int i = 0; i < arrays.length; i++) {
System.out.println(arrays[i]);
}
int[] result = new int[arrays.length];
//反转数组
for(int i = 0,j=arrays.length-1;i<arrays.length; i++,j--){
result[j] = arrays[i];
}
//取不到下标
for (int r : result) {
System.out.println(r);
}
}
}
多维数组
package com.xie.array;
public class Demo02 {
public static void main(String[] args) {
int[][] arrays = {{1,2},{3,4},{5,6}};
for (int i = 0; i < arrays.length; i++) {
for (int j = 0; j < arrays[i].length; j++) {
System.out.println(arrays[i][j]);
}
}
}
}
Arrays类
package com.xie.array;
import java.util.Arrays;
public class Demo03 {
public static void main(String[] args) {
int[] a = {1,23,445,111,33333,42,12};
//打印
System.out.println(Arrays.toString(a));
//排序
Arrays.sort(a);
System.out.println(Arrays.toString(a));
//填充
Arrays.fill(a,2,4,0);
System.out.println(Arrays.toString(a));
}
}
冒泡排序
package com.xie.array;
import java.util.Arrays;
public class Demo04 {
public static void main(String[] args) {
int[] a = {1,33,2,334,3333,3,9};
int temp;
for (int i = 0; i < a.length-1; i++) {
for (int j = 0; j < a.length-i-1; j++) {
if(a[j]>a[j+1]){
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
System.out.println(Arrays.toString(a));
}
}
这篇关于第九话-Java数组的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!