• java


     1 package day05;
     2 import java.util.Arrays;
     3 import java.util.Random;
     4 
     5 /*
     6  * 附:如果需要使用引用类,如Random类的方法,
     7  *    需要新建(new)引用变量,比如新建  Random型变量 rand,
     8  *      而后,才能调用Random类的方法rand.nextInt()。     
     9  *  
    10  */
    11 //扩容输出最大数
    12 public class ArrayCopyDemo {
    13     public static void main(String args[]) {
    14         int[] a = new int[6];
    15         Random rand = new Random();
    16         
    17         //用随机数对数组各元素初始化,并遍历数组
    18         for(int i=0;i<a.length;i++) {
    19             a[i] = (int)(rand.nextInt(100));
    20             System.out.print(a[i]+",");
    21         }
    22         System.out.println();
    23         
    24         
    25         //数组扩容
    26         a = Arrays.copyOf(a, a.length+1);
    27         
    28         for(int i=0;i<a.length;i++) {            
    29             System.out.print(a[i]+",");        
    30         }
    31         System.out.println();
    32         
    33         //找最大数
    34         int  max = a[0];
    35         for(int i=1;i<a.length;i++) {
    36             if(a[i]>max) {
    37                 max = a[i];
    38             }
    39         }
    40         a[a.length-1] = max;
    41         
    42         System.out.println("输出新数组:");        
    43         for(int i=0;i<a.length;i++) {            
    44             System.out.print(a[i]+",");        
    45         }
    46         
    47         /*
    48          * 利用system.arraycopy( , , , )完成数组的扩
    49          * 容,找最大数,遍历
    50          * 
    51         //数组扩容,遍历
    52         int[] a1 = new int[7];
    53         System.arraycopy(a, 0, a1, 0, 5);
    54         
    55         for(int i=0;i<a1.length;i++) {            
    56             System.out.print(a1[i]+",");        
    57         }
    58         System.out.println();
    59         
    60         int  max = a1[0];
    61         for(int i=1;i<a1.length;i++) {
    62             if(a1[i]>max) {
    63                 max = a1[i];
    64             }
    65         }
    66         a1[a1.length-1] = max;
    67         
    68         System.out.println("输出新数组:");
    69         
    70         for(int i=0;i<a1.length;i++) {            
    71             System.out.print(a1[i]+",");        
    72         }
    73         */
    74     }
    75 }
  • 相关阅读:
    Hibernate框架简介
    [leecode]Evaluate Reverse Polish Notation
    linux 服务器之间配置免密登录
    大数据学习系列之一 ----- Hadoop环境搭建(单机)
    Hadoop hbase集群断电数据块被破坏无法启动
    CentOS 6 上安装 pip、setuptools
    CentOs6.7 python2.6升级到2.7.11
    安装phantomjs(Ubuntu版本 MacOS版本)
    Linux/Centos下安装部署phantomjs 及使用
    linux 查看系统磁盘、内存大小
  • 原文地址:https://www.cnblogs.com/DeRozan/p/6781320.html
Copyright © 2020-2023  润新知