• [Java] 数组-02 初始化与length


    数组
     *1 动态初始化
     *2 静态初始化
     数组元素的默认初始化
      ** 数组是引用类型,它的元素相当于类的成员变量,因此数组分配空间后,每个元素也被按照成员变量的规则被隐式初始化。
     数组有一个属性 length  指明它的长度
    package com.bjsxt.chap5;
    
    public class TestArray {
    
        public static void main(String[] args) {
            int[] a = {1, 2, 3, 4, 5, 6, 7};
            for (int i = 0; i < a.length; i++) {
                System.out.print(a[i] + " ");
            }
            System.out.println();
            for (int i = 0; i < args.length; i++) {
                System.out.print(args[i] + " "); // 命令行参数
            }
            System.out.println();
        }
    }
    package com.bjsxt.chap5;
    
    public class TestArgs {
        public static void main(String[] args) {
    
            if (args.length < 3) {
                System.out.println("Usage: java Test "n1" "op" "n2"");
                System.exit(-1);
            }
            double d1 = Double.parseDouble(args[0]);
            double d2 = Double.parseDouble(args[2]);
            double d = 0;
            if (args[1].equals("+"))
                d = d1 + d2;
            else if (args[1].equals("-"))
                d = d1 - d2;
            else if (args[1].equals("x"))
                d = d1 * d2; // * 号在 dos 下有特殊的含义
            else if (args[1].equals("/"))
                d = d1 / d2;
            else {
                System.out.println("Error operator!");
                System.exit(-1);
            }
            System.out.println(d);
        }
    }
    package com.bjsxt.chap5;
    
    public class NumSort {
        public static void main(String[] args) {
            int[] a = new int[args.length];
            for (int i = 0; i < args.length; i++) {
                a[i] = Integer.parseInt(args[i]);
            }
            print(a);
            selectionSort(a);
            print(a);
        }
    
        private static void selectionSort(int[] a) {
            int k, temp;
            for (int i = 0; i < a.length; i++) {
                k = i;
                for (int j = k + 1; j < a.length; j++) {
                    if (a[j] < a[k]) {
                        k = j;
                    }
                }
    
                if (k != i) {
                    temp = a[i];
                    a[i] = a[k];
                    a[k] = temp;
                }
            }
        }
    
        private static void print(int[] a) {
            for (int i = 0; i < a.length; i++) {
                System.out.print(a[i] + " ");
            }
            System.out.println();
        }
    }


    
    
    
    
    
    
    
  • 相关阅读:
    2020年捌月份生活随笔
    2020年柒月份生活随笔
    2020年陆月份生活随笔
    第二次:郑州银行杯|2019郑州国际马拉松
    第一次:海尔|2017年青岛马拉松
    专项测试技能和线上线下监控
    实用
    Oracle 数据库 有用的sql语句
    Qt demo
    springboot demo
  • 原文地址:https://www.cnblogs.com/robbychan/p/3786579.html
Copyright © 2020-2023  润新知