zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    hdu 1083 Courses
    hdu 1068 Girls and Boys
    hdu 2988 Dark roads
    hdu 1879 继续畅通工程
    hdu 1875 畅通工程再续
    hdu 1233 还是畅通工程
    hdu 4040 (贪心算法)
    FZU 2111 Min Number
    Reconstructing Cloud-Contaminated Multispectral Images With Contextualized Autoencoder Neural Networks(自编码机重建云污染区)
    跑深度学习网络遇到的一些错误总结
  • 原文地址:https://www.cnblogs.com/DeRozan/p/6781320.html
Copyright © 2011-2022 走看看