zoukankan      html  css  js  c++  java
  • java中的数组概念

    数组的定义形式:

     动态初始化方式;

      1.声明并开辟数组

    String str[]=new String[3];//3表示数组的长度

      2.分布完成

    String str[]=null;
    str=new String[3];

       静态初始化

      1.简化格式

    String str [] = {"1","2","3"};

      2.完整格式

    String str = new String[]{"1","2","3"}

    数组的内存分析,数组属于引用传递

    public class Test{
        public static void main(String args[]){
            int data[] = new int[3];//开辟一个长度为3的数组
            data[0]=1;
            data[1]=2;
            data[2]=3;

          int temp[] = data;
          temp[0] = 99

          for(int x=0;x<data.length;x++){

                System.out.println(data[x]);
            }
        }
    }

    当temp[0]的值改变,则data[0]也会被改变,这就是引用传递;

    二维数组的定义

    public class Test{
        public static void main(String args[]){
            int data[][] = new int[][]{
                {1,2,3},
                {4,5,6},
                {7,8,9}
            };
            //进行数组的输出操作
            //外层循环控制行
            for(int x=0;x<data.length;x++){
                //内层循环控制列
                for(int y=0;y<data[x].length;y++){
                    System.out.print(data[x][y]+"	");
                }
                System.out.println();
            }
        }
    }

    数组排序中的一个方法

    public class Test{
        public static void main(String args[]){
            int data[] = new int[]{10,25,3,40,8,27,44,17,13,50,2};
            print(data);
            sort(data);
            print(data);
        }
        public static void sort(int arr[]){
            for(int x=0;x<arr.length;x++){//外层循环主要控制排序的次数
                for(int y=0;y<arr.length-1;y++){//内层循环主要是进行数组的排序
                    if(arr[y]>arr[y+1]){
                        int temp=arr[y];
                        arr[y] = arr[y+1];
                        arr[y+1]=temp;
                    }
                    
                }
            }
        }
        public static void print(int temp[]){
            for(int x=0;x<temp.length;x++){
                System.out.print(temp[x]+"");
            }
            System.out.println();
        }
    }

    数组的转置

    //数组的转置
    public class Test{
        public static void main(String args[]){
            int data[] = new int[]{1,2,3,4,5,6,7,8,9,10};
            print(data);
            reverse(data);
            print(data);
        }
        public static void reverse(int arr[]){
            int head=0;        //从头开始增加
            int tail=arr.length-1;    //从尾部递减
            for(int x=0;x<arr.length/2;x++){
                int t=arr[head];
                arr[head] = arr[tail];
                arr[tail] = t;
                tail--;
                head++;
            }
        }
        public static void print(int temp[]){
            for(int x=0;x<temp.length;x++){
                System.out.print(temp[x]+"、");
            }
            System.out.println();
        }
    }

    二维数组转置

    public class Test{
        public static void main(String args[]){
            int data[][] = new int[][]{
                {1,2,3},
                {4,5,6},
                {7,8,9}
            };
            print(data);
            reverse(data);
            print(data);
        }
        public static void reverse(int arr[][]){
            for(int x=0;x<arr.length;x++){
                for(int y=x;y<arr[x].length;y++){
                        int t=arr[x][y];
                        arr[x][y]=arr[y][x];
                        arr[y][x] = t;
                }
            }
        }
        public static void print(int temp[][]){
            for(int x= 0;x<temp.length;x++){
                for(int y=0;y<temp[x].length;y++){
                    System.out.print(temp[x][y]+"、");
                }
                System.out.println();
            }
        }
    }

    数组中常用的方法

    数组拷贝:System.arraycopy

    public class TestDemo{
        public static void main(String args[]){
            int dataA[] =new int[]{1,2,3,4,5,6,7,8,9};//源数组
            int dataB[] =new int[]{11,22,33,44,55,66,77,88,99};//拷贝的数组
            System.arraycopy(dataA,2,dataB,3,2);
            print(dataB);
        }
        public static void print(int temp[]){
            for(int x= 0;x<temp.length;x++){
                    System.out.print(temp[x]+"、");        
            }
            System.out.println();
        }
    }
    11、22、33、3、4、66、77、88、99、

    数组排序的方法:java.util.Array.sort(数组名称);

    初次接触对象数组:所谓的对象数组,就是把引用类型的对象通过数组的形式进行保存

    代码实例

    class Book{
        private String title;
        private double price;
        public Book(String title, double  price){
            this.title=title;
            this.price=price;
        }
        public String toString(){
            return "书的名字:" + this.title + ",价格:"  + this.price;
        }
    }
    public class Test{
        public static void main(String args[]){
            Book book[] = new Book[]{
                new    Book("java",11.3),
                new    Book("javascript",17.3),
                new    Book("html",9.3)
            };
            for(int x=0;x<book.length;x++){
                System.out.println(book[x]);
            }
        }
    }
  • 相关阅读:
    阅读 video in to axi4-stream v4.0 笔记
    python 字符串操作
    python 基本语句
    Python 算术运算符
    芯片企业研报阅读
    量化分析v1
    基于MATLAB System Generator 搭建Display Enhancement模型
    System Generator 生成IP核在Vivado中进行调用
    FPGA 中三角函数的实现
    System Generator 使用离散资源
  • 原文地址:https://www.cnblogs.com/hu1056043921/p/7264404.html
Copyright © 2011-2022 走看看