一维数组

1
写一段程序来阐述数组简单的应用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package com.liaojianya.chapter1;
 
import java.util.Random;
 
/**
 * This program demonstrates the use of array.
 * @author LIAO JIANYA
 * 2016年7月20日
 */
public class ArrayAssignment8_3
{
    public static void main(String[] args)
    {
        Random rand = new Random();
        int[] a = null;
        int[] b = null;
        a = new int[rand.nextInt(10) + 1];
        b = a;
         
        System.out.println("the length of array_a : " + a.length);
        System.out.println("the length of array_b : " + b.length + "\n");
         
        for(int i = 0; i < a.length; i++)
        {
            a[i] = rand.nextInt(100);
            System.out.print("a[" + i + "] = " + a[i] + "\t");
            System.out.println("b[" + i + "] = " + b[i]);
        }
         
        if(a.length == 1)
        {
            System.out.println("The array_a only has one element.\n"
                    + "So the max value of array_a is same as the min value!");
        }
        else
        {
            int max = a[0];
            int min = a[0];
             
             
            for(int i = 0; i < a.length; i++)
            {
                if(a[i] > max)
                {
                    max = a[i];
                }
                 
                else if(a[i] < min)
                {
                    min = a[i];
                }
            }
             
            System.out.println("\nThe max value of array_a is : " + max);
            System.out.println("The min value of array_a is : " + min);
             
        }
         
 
    }
}

  运行结果1:

1
2
3
4
5
6
the length of array_a : 1
the length of array_b : 1
 
a[0] = 70   b[0] = 70
The array_a only has one element.
So the max value of array_a is same as the min value!

  运行结果2:

1
2
3
4
5
6
7
8
9
10
11
12
13
the length of array_a : 7
the length of array_b : 7
 
a[0] = 39   b[0] = 39
a[1] = 90   b[1] = 90
a[2] = 49   b[2] = 49
a[3] = 35   b[3] = 35
a[4] = 33   b[4] = 33
a[5] = 12   b[5] = 12
a[6] = 75   b[6] = 75
 
The max value of array_a is : 90
The min value of array_a is : 12

  分析:

  1)代码行:b = a;表示将a数组的引用赋值给数组b,这时a和b是指向同一个数组对象。即“一个数组,两个名字”

  2)创建一个Random类型的对象rand,此对象可以灵活的产生随机数,代码行a = new int[rand.nextInt(10)+1]; 在rand.nextInt()后加1,可以避免出现0个元素数组。

若不加1,若随机到0,则会显示:

1
2
3
4
5
the length of array_a : 0
Exception in thread "main" the length of array_b : 0
 
java.lang.ArrayIndexOutOfBoundsException: 0
    at com.liaojianya.chapter1.ArrayAssignment8_3.main(ArrayAssignment8_3.java:30)

  3)将变量min与max初值设为数组的第一个元素后,再逐一的与数组中的各元素相比,比min小的就将该元素值赋给min存放,使得min中的值始终保持最小;同理,当该元素大于max时,就将该元素赋给max存放,使得max的值保持最大。

posted @   Andya_net  阅读(145)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示