首页 > 代码库 > java基础---->数组的基础使用(一)

java基础---->数组的基础使用(一)

  数组是一种效率最高的存储和随机访问对象引用序列的方式,我们今天来对数组做简单的介绍。

数组的简单使用

一、数组的赋值

String[] arrays = new String[]{"linux", "huhx", "android"};//arrays[4] = "tomhu"; // 数组越界String[] arrays2 = arrays;arrays2[1] = "chenhui"; // 修改arrays2的值,对arrays产生了影响System.out.println(Arrays.toString(arrays)); // [linux, chenhui, android]arrays[2] = "google";System.out.println(Arrays.toString(arrays2)); // [linux, chenhui, google]

 

二、数组的深复制

String[] arrays3 = Arrays.copyOf(arrays, arrays.length); // 底部实现仍然是System.arrarycopy()方法System.out.println(Arrays.toString(arrays3)); // [linux, chenhui, google]arrays3[1] = "nodeJS"; // 修改arrays3的值,对arrays没有影响System.out.println(Arrays.toString(arrays)); // [linux, chenhui, google]

 

三、数组的填充

String[] arrays4 = new String[4];Arrays.fill(arrays4, "linux");System.out.println(Arrays.toString(arrays4)); // [linux, linux, linux, linux]

 

四、数组内容的比较

boolean arraysEqual = Arrays.equals(arrays3, arrays);System.out.println(arraysEqual); // falseSystem.out.println(Arrays.equals(arrays, arrays2)); // true

 

五、数组的排序,改变了数组本身

Arrays.sort(arrays);System.out.println(Arrays.toString(arrays)); // [chenhui, google, linux]

 

六、数组内元素的查找

int location = Arrays.binarySearch(arrays, "linux");System.out.println(location); // 2

 

七、数组生成ArrayList

List<String> strings = Arrays.asList(arrays);Iterator<String> iterator = strings.iterator();while (iterator.hasNext()) {    System.out.print(iterator.next());}

 

对Arrays类方法的分析

一、fill填充方法

public static void fill(Object[] a, Object val) {    for (int i = 0, len = a.length; i < len; i++)        a[i] = val;}

二、toString打印方法

public static String toString(Object[] a) {    if (a == null)        return "null";    int iMax = a.length - 1;    if (iMax == -1)        return "[]";    StringBuilder b = new StringBuilder();    b.append(‘[‘);    for (int i = 0; ; i++) {        b.append(String.valueOf(a[i]));        if (i == iMax)            return b.append(‘]‘).toString();        b.append(", ");    }}

三、equal比较方法

public static boolean equals(Object[] a, Object[] a2) {    if (a==a2)        return true;    if (a==null || a2==null)        return false;    int length = a.length;    if (a2.length != length)        return false;    for (int i=0; i<length; i++) {        Object o1 = a[i];        Object o2 = a2[i];        if (!(o1==null ? o2==null : o1.equals(o2)))            return false;    }    return true;}

四、asList转换成列表的方法

@SafeVarargspublic static <T> List<T> asList(T... a) {    return new ArrayList<>(a);}

五、iterator遍历方法

public Iterator<E> iterator() {    return new Itr();}/** * An optimized version of AbstractList.Itr */private class Itr implements Iterator<E> {    int cursor;       // index of next element to return    int lastRet = -1; // index of last element returned; -1 if no such    int expectedModCount = modCount;    public boolean hasNext() {        return cursor != size;    }    @SuppressWarnings("unchecked")    public E next() {        checkForComodification(); // 检查数组内容的一致性        int i = cursor;        if (i >= size)            throw new NoSuchElementException();        Object[] elementData = ArrayList.this.elementData;        if (i >= elementData.length)            throw new ConcurrentModificationException();        cursor = i + 1;        return (E) elementData[lastRet = i];    }    public void remove() {        if (lastRet < 0)            throw new IllegalStateException();        checkForComodification();        try {            ArrayList.this.remove(lastRet);            cursor = lastRet;            lastRet = -1;            expectedModCount = modCount;        } catch (IndexOutOfBoundsException ex) {            throw new ConcurrentModificationException();        }    }    final void checkForComodification() {        if (modCount != expectedModCount)            throw new ConcurrentModificationException();    }}

 

友情链接

 

java基础---->数组的基础使用(一)