111 发表于 2017-7-2 12:03:09

数组基础练习

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+",");
}
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+",");
}
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.length;y++){
exchange=temp;
temp=temp;
temp=exchange;
}
}
return temp;
}
//查找数组中的数字
public static boolean search(int[] temp,int num){
for(int i=0;i<temp.length;i++){
if(temp==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>temp){
x=temp;
temp=temp;
temp=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;
temp=temp;
temp=exchange;
}
}else{
for(int j=0;j<Math.round(temp.length/2);j++){
exchange=temp;
temp=temp;
temp=exchange;
}
}
return temp;
}
}
页: [1]
查看完整版本: 数组基础练习