• Java知识系统回顾整理01基础06数组02初始化数组


    一、分配空间与赋值分步进行

    分配空间与赋值分步进行

    public class HelloWorld {

        public static void main(String[] args) {

            int[] a = new int[5]; //分配了长度是5的数组,但是没有赋值
            

            //没有赋值,那么就会使用默认值

            //作为int类型的数组,默认值是0

            System.out.println(a[0]);

              

            //进行赋值   

            a[0] = 100;

            a[1] = 101;

            a[2] = 103;

            a[3] = 120;

            a[4] = 140;

        }

    }

       

    二、分配空间,同时赋值

    分配空间,同时赋值

    public class HelloWorld {

        public static void main(String[] args) {

            //写法一: 分配空间同时赋值

            int[] a = new int[]{100,102,444,836,3236};

            //写法二: 省略了new int[],效果一样

            int[] b = {100,102,444,836,3236};        

            //写法三:同时分配空间,和指定内容

            //在这个例子里,长度是3,内容是5个,产生矛盾了

            //所以如果指定了数组的内容,就不能同时设置数组的长度

            int[] c = new int[3]{100,102,444,836,3236};        

        }

    }

       

    三、练习--数组反转

    题目:

    首先创建一个长度是5的数组,并填充随机数。 (向数组填充随机数的办法,参考这里)

    参考:使用for循环或者while循环,对这个数组实现反转效果

       

    官方答案:

    public class HelloWorld {

        public static void main(String[] args) {

            int[] a = new int[5];

            a[0] = (int) (Math.random() * 100);

            a[1] = (int) (Math.random() * 100);

            a[2] = (int) (Math.random() * 100);

            a[3] = (int) (Math.random() * 100);

            a[4] = (int) (Math.random() * 100);

       

            System.out.println("数组中各个值是:");

            for (int i = 0; i < a.length; i++)

                System.out.print(a[i] + " ");

              

            /*思路一: 使用临时数组*/

              

            System.out.println();

            //准备临时数组

            int[] temp = new int[a.length];

            //把原数组的内容复制给临时数组

            for (int i = 0; i < temp.length; i++) {

                temp[i] = a[i];

            }

            System.out.println("临时数组中的各个值是:");

            for (int i = 0; i < temp.length; i++)

                System.out.print(temp[i] + " ");

            System.out.println();

            //反转的做法是把临时数组的数据,挨个 倒 放入 原数组中

            for (int i = 0; i < temp.length; i++) {

                a[i] = temp[temp.length-1-i];

            }

        

            System.out.println("反转后的数组中各个值是:");

            for (int i = 0; i < a.length; i++)

                System.out.print(a[i] + " ");

              

            System.out.println();

              

            /*思路二: 进行首尾调换*/      

            for (int i = 0; i < a.length/2; i++) {

                int middle = a[a.length-i-1];

                a[a.length-i-1] = a[i];

                a[i] = middle;

            }      

            System.out.println("再次反转后的数组中各个值是:");

            for (int i = 0; i < a.length; i++) {

                System.out.print(a[i] + " ");

            }

            System.out.println();      

        }

    }

       

       

  • 相关阅读:
    【.net】未在本地计算机上注册“microsoft.ACE.oledb.12.0”提供程序解决办法
    【JS】JavaScript中Null和undefind区别
    【SQL】SQL中on条件与where条件的区别
    【C#】C#创建自定义Object对象
    【.NET】asp.net Redirect 图片路径
    【JQ】jq动态绑定事件.on()、解绑事件off()
    【.NET】using 语句中使用的类型必须可隐式转换为"System.IDisposable"
    C# enum、int、string三种类型互相转换
    js中Date与timestamp(时间戳)的相互转换
    2. 自动化运维系列之Cobbler给Openstack节点安装操作系统。
  • 原文地址:https://www.cnblogs.com/xlfcjx/p/10773822.html
Copyright © 2020-2023  润新知