二维数组的打印,查找等
二维数组的打印
public class TwoDimensionalArray { public static void main(String[] args) { int x = 0; int[][] a = { { 8, 2, 6, 5 }, { 6, 3, 1, 0 }, { 8, 7, 9, 6 } }; System.out.print("\t \t"); for (int i = 0; i <= a.length; i++) { System.out.print(i + "\t"); } System.out.print("\n\t" + "-------------------------"); System.out.println(); for (int i = 0; i < a.length; i++) { System.out.print(i + " |\t"); for (int j = 0; j < a[i].length; j++) { System.out.print(a[i][j] + "\t"); // "\t"是制表符 } System.out.println(); } } }
public class TwoDimensionalArray { public static void main(String[] args) { int[][] a = {{8, 2, 6, 5}, {6, 3, 1, 0}, {8, 7, 9, 6}}; for (int i=0;i<3;i++) System.out.println ( Arrays.toString (a[i])); } }
二维数组的查找
public class Array { public static boolean find(int [][] arry,int num){ int column=arry[0].length-1; int row=0; while(row<arry.length&&column>=0){ if(num==arry[row][column]){ return true; } if(arry[row][column]>num){ column--; }else{ row++; } } return false; } public static void main(String[] args) { int [][] arry=new int[3][3]; for(int i=0;i<arry.length;i++){ for(int j=0;j<arry[0].length;j++){ arry[i][j]=(i+1)*(j+1); } } System.out.println("查看是否有元素9:"+find(arry,9)); System.out.println("查看是否有元素10:"+find(arry,10)); } }
public class TwoDimensionalArray { int[][] a = {{8, 2, 6, 5}, {6, 3, 1, 0}, {8, 7, 9, 6}}; public static boolean find(int[][] arry, int num) { int column = arry[0].length - 1; int row = 0; while (row < arry.length && column >= 0) { if (num == arry[row][column]) { return true; } if (arry[row][column] > num) { column--; } else { row++; } } return false; } public static void print(int[][] a) { int x = 0; System.out.print("\t \t"); for (int i = 0; i <= a.length; i++) { System.out.print(i + "\t"); } System.out.print("\n\t" + "-------------------------"); System.out.println(); for (int i = 0; i < a.length; i++) { System.out.print(i + " |\t"); for (int j = 0; j < a[i].length; j++) { System.out.print(a[i][j] + "\t"); // "\t"是制表符 } System.out.println(); } } public static void main(String[] args) { int[][] arry = new int[3][3]; for (int i = 0; i < arry.length; i++) { for (int j = 0; j < arry[0].length; j++) { arry[i][j] = (i + 1) * (j + 1); } } print(arry); System.out.println(); System.out.println("查看是否有元素9:" + find(arry, 9)); System.out.println("查看是否有元素10:" + find(arry, 10)); } }
偶数
public class OddTest { public static void main(String[] args) { boolean odd = true;// 奇数行标志 for (int i = 2; i <= 50; i += 2) { System.out.print(odd ? i + "\tA\t" : "A\t" + i + "\t"); if (i % 10 == 0) { System.out.println(); odd = !odd; } } } }
------------------------- A little Progress a day makes you a big success... ----------------------------