zoukankan      html  css  js  c++  java
  • Java中的数组操作进阶

    package com.mi.array;
    
    import java.util.Arrays;
    
    /**
     * System.arraycopy(i, 0, j, 0, i.length);这种复制会覆盖目标数组元素
     * 数组的copy
     * @author admin
     *
     */
    public class CopyArray {
    
        public static void main(String[] args) {
            int[] i =new int[7];
            int[] j = new int[10];
    
            Arrays.fill(i,47);
            Arrays.fill(j,99);
            System.out.println("i= "+Arrays.toString(i));
            System.out.println("j= "+Arrays.toString(j));
            //参数(原数组,原数组的开始位置,目标数组,目标数组的开始位置,需要复制的元素的个数)
            System.arraycopy(i, 0, j, 0, i.length);
            System.out.println("j= "+Arrays.toString(j));
            
            /**
             * 结果
             *  i= [47, 47, 47, 47, 47, 47, 47]
                j= [99, 99, 99, 99, 99, 99, 99, 99, 99, 99]
                j= [47, 47, 47, 47, 47, 47, 47, 99, 99, 99]
    
             */
        }
    }
    package com.mi.array;
    
    import java.util.Arrays;
    
    /**
     * 
     *数组的比较/数组元素的比较
     *Arrays.equals 条件:1,数组长度相等;2,数组对应位置的值必须相等
     */
    public class CompareArray {
    
        public static void main(String[] args) {
            int[] a1=new int[10];
            int[] a2=new int[10];
            //Arrays.fill也可以给数组的特定区间传值,比如 Arrays.fill(a1,2,4 47);
            //同样,Arrays.sort可以给特定区间排序,比如 Arrays.sort(a1,27);
            Arrays.fill(a1, 47);
            Arrays.fill(a2, 47);
            
            System.out.println(Arrays.equals(a1, a2));
            
        }
    }
    package com.mi.array;
    
    import java.util.*;
    /**
     * 数组排序/在已排序的数组中查找
     * @author admin
     *
     */
    public class StringSort {
        public static void main(String[] args) {
            int[] a1=new int[10];
            //返回该值在数组中的索引,若不存在,则返回-1
            int index = Arrays.binarySearch(a1, 2);
        }
    }
  • 相关阅读:
    tomcat配置虚拟主机
    android widget 开发实例 : 桌面便签程序的实现具体解释和源代码 (上)
    Delphi 2007体验!
    ACE定时器
    Unity--关于优化方面的那些事儿(一)
    LTP介绍
    Java正則表達式入门
    Spring整合Hibernate的步骤
    ThreadPool.QueueUserWorkItem的性能问题
    用Bootstrap 写了个站点
  • 原文地址:https://www.cnblogs.com/tingbogiu/p/5801885.html
Copyright © 2011-2022 走看看