标签:nbsp bsp system integer ann imp for exti int()
1.一维矩阵的动态定义(代码注释)
1.1方法一
package dongtai; import java.util.Scanner; import java.util.ArrayList; public class DongTai { public static void main(String[] args) { Scanner scan = new Scanner(System.in); ArrayList<Integer> p = new ArrayList<>(); //创建一个ArrayList int n = scan.nextInt(); for(int i=0; i<n+1; i++) { p.add(0); //往p数组里面添加元素 } for(int j=0; j<n+1; j++) { p.set(j, scan.nextInt()); //键盘端输入数组的元素大小 }
1.2方法二(多维数组也可用这种方法)
package dongtai; import java.util.Scanner; public class DongTai { public static int []p; //直接定义一个一维数组 public static void main(String[] args) { Scanner scan = new Scanner(System.in); int num = scan.nextInt(); p = new int[num]; //可直接定义数组大小 } }
2.二维数组
package dongtai; import java.util.Scanner; public class DongTai { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int n = scan.nextInt(); int [][] m; m = new int[n][]; for(int i=1; i<n; i++) //前两个n位第一维,后两个n为第二维 { m[i] = new int[n]; //动态创建第二维 for(int j=1; j<n; j++) { m[i][j] = 0; } } } }
3.二维数组与多维数组(1.2方法二)
package dongtai; import java.util.Scanner; public class DongTai { public static int arr[][][]; public static void main(String[] args) { Scanner scan = new Scanner(System.in); int n = scan.nextInt(); arr = new int[n][n][n]; } }
标签:nbsp bsp system integer ann imp for exti int()
原文地址:https://www.cnblogs.com/sped-/p/12838803.html