• 数组元素的填充与替换、排序和复制


    import java.util.Arrays;
    public class Copy2 {
    	public static void main(String [] args){
    		//数组填充       fill(要元素替换的数组int[]a,填充的值int value)
    		int []a = new int [6];
    		Arrays.fill(a,8);					//将a数组中所有元素填为8
    		for(int n:a){						//foreach遍历数组
    			System.out.print(n+" ");
    		}
    		System.out.println();
    		
    		
    		//指定填充的范围       fill(int[]a,起始int fromIndex,终止int toIndex,int value)
    		Arrays.fill(a,1,5,0);			//指定把下标为1-5的元素替换为0(不包括5)
    		for(int n:a){
    			System.out.print(n+" ");
    		}
    		System.out.println();
    		System.out.println();
    		System.out.println("数组排序");
    		
    		//数组排序     sort(要排序的数组)
    		int b[]={23,45,12,65,2,11,0,45};           //定义数组
    		for(int n:b){
    			System.out.print(n+" ");
    		}
    		System.out.println();
    		
    		Arrays.sort(b);				  //调用排序方法
    		for(int n:b){
    			System.out.print(n+" ");
    		}
    		System.out.println();
    		System.out.println();
    		
    		
    		
    		System.out.println("数组的复制:");
    		//数组的复制    
    		int [] newb=Arrays.copyOf(b,5);             //当b.length>newb.length时,将b数组的前5个元素复制到newb数组
    		for(int n:newb){
    			System.out.print(n+" ");
    		}
    		System.out.println();
    		
    		int [] newb1 = Arrays.copyOf(b, 14);          //当b.length<newb1.length时,空余的元素位用0填充
    		for(int n:newb1){
    			System.out.print(n+" ");
    		}
    		System.out.println();
    		
    		int [] newb2 = Arrays.copyOfRange(b, 2, 7);	//将b数组下标为2-7的元素复制到newb2数组中
    		for(int n:newb2){
    			System.out.print(n+" ");
    		}
    		System.out.println();
    		System.out.println();
    	
    		
    		
    	}
    
    }
    

      

  • 相关阅读:
    hdu5081
    hdu5079
    hdu5076
    hdu5072
    codeforces 739E
    codeforces 739D
    2017.2其他简要题解
    spring事务传播回滚策略
    mybatis一级缓存和二级缓存
    spring-boot-mybatis-starter工作原理
  • 原文地址:https://www.cnblogs.com/-maji/p/7071257.html
Copyright © 2020-2023  润新知