public class testDemo { public static void main(String[] args) { // TODO Auto-generated method stub int data[][] = new int[][]{{1,2,3,4},{5,6,7,8},{9,3,6,1},{8,2,2,8}}; for(int row=0;row<4;row++){ for(int col=0;col<4;col++){ System.out.print(data[row][col]+","); } System.out.println(); } System.out.println(); transpose(data); for(int i=0;i<4;i++){ for(int j=0;j<4;j++){ System.out.print(data[i][j]+","); } System.out.println(); } } //二维数组行列转置 public static int[][] transpose(int[][] temp){ int exchange=0; for(int x=0;x<temp.length;x++){ for(int y=x;y<temp[x].length;y++){ exchange=temp[x][y]; temp[x][y]=temp[y][x]; temp[y][x]=exchange; } } return temp; } //查找数组中的数字 public static boolean search(int[] temp,int num){ for(int i=0;i<temp.length;i++){ if(temp[i]==num){ return true; } } return false; } //排序 public static int[] sort(int[] temp){ int x=0; for(int n=0;n<temp.length;n++){ for(int i=0;i<temp.length-1;i++){ if(temp[i]>temp[i+1]){ x=temp[i]; temp[i]=temp[i+1]; temp[i+1]=x; } } } return temp; } //一维数组反转 public static int[] convert(int[] temp){ int exchange=0; if(temp.length%2==0){ for(int i=0;i<temp.length/2;i++){ exchange=temp[i]; temp[i]=temp[temp.length-i-1]; temp[temp.length-i-1]=exchange; } }else{ for(int j=0;j<Math.round(temp.length/2);j++){ exchange=temp[j]; temp[j]=temp[temp.length-j-1]; temp[temp.length-j-1]=exchange; } } return temp; } }
时间: 2024-10-17 02:12:39