• 数组基础练习


    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;
        }
    
    }
  • 相关阅读:
    selenium爬取沃尔玛分类和商品详情
    linux下部署redis
    linux系统基本操作命令
    Scrapy & Django项目
    爬虫爬取百度搜狗图片持久化存储
    django + vue 简单配置登录注册及分页和实例化
    vue安装及常用属性
    vue跨域配制
    Django中配置全文检索
    Django框架知识点简单总结(1)
  • 原文地址:https://www.cnblogs.com/redick/p/6349169.html
Copyright © 2020-2023  润新知