Java基础——数组
2021-03-28 21:26
标签:com class main out 图片 stat img for java 文章摘自: https://www.liaoxuefeng.com/wiki/1252599548343744/1259543088592672 1.遍历数组的三种方式: 2.数组的排序 对数组的排序实际上修改了数组本身。 3.二维数组 二维数组的每个数组元素的长度并不要求相同,例如,可以这么定义 遍历二维数组: Java基础——数组 标签:com class main out 图片 stat img for java 原文地址:https://www.cnblogs.com/William-xh/p/13620892.htmlpublic class Main {
public static void main(String[] args) {
int[] ns = { 1, 4, 9, 16, 25 };
for (int i=0; i
public class Main {
public static void main(String[] args) {
int[] ns = { 1, 4, 9, 16, 25 };
for (int n : ns) {
System.out.println(n);
}
}
}
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] ns = { 1, 1, 2, 3, 5, 8 };
System.out.println(Arrays.toString(ns));
}
}
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] ns = { 28, 12, 89, 73, 65, 18, 96, 50, 8, 36 };
Arrays.sort(ns);
System.out.println(Arrays.toString(ns));
}
}
public class Main {
public static void main(String[] args) {
int[][] ns = {
{ 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 }
};
int[] arr0 = ns[0];
System.out.println(arr0.length); // 4
}
}
ns
数组:int[][] ns = {
{ 1, 2, 3, 4 },
{ 5, 6 },
{ 7, 8, 9 }
};
for (int[] arr : ns) {
for (int n : arr) {
System.out.print(n);
System.out.print(‘, ‘);
}
System.out.println();
}
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[][] ns = {
{ 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 }
};
System.out.println(Arrays.deepToString(ns));
}
}
上一篇:07_线程池