數組基礎練習

來源:互聯網
上載者:User

標籤:convert   blog   search   數組   i+1   i++   arc   static   oid   

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;    }}

 

數組基礎練習

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.