我们知道,在Java中,数组也是一种对象,所以,数组应该也存储在堆区中。
既然数组是一种对象,那么它也应该有相应的方法和属性,通过IDE的帮助,我们可以得到以下方法:
根据猜测,数组应该直接继承Object类,并且复写了clone方法(测试过,应该是浅复制)。但是没有复写finalize方法(所以一共也就只有10种方法)
属性只有一个,即length,表明该数组的元素大小。
数组和集合的转换:
如果想把集合转换成数组,可以通过接口List的toArray方法进行。
/** * Returns an array containing all of the elements in this list in proper * sequence (from first to last element). * * <p>The returned array will be "safe" in that no references to it are * maintained by this list. (In other words, this method must * allocate a new array even if this list is backed by an array). * The caller is thus free to modify the returned array. * * <p>This method acts as bridge between array-based and collection-based * APIs. * * @return an array containing all of the elements in this list in proper * sequence * @see Arrays#asList(Object[]) */ Object[] toArray();
数组转换成集合。
Arrays.asList(T... t);
/** * Returns a fixed-size list backed by the specified array. (Changes to * the returned list "write through" to the array.) This method acts * as bridge between array-based and collection-based APIs, in * combination with {@link Collection#toArray}. The returned list is * serializable and implements {@link RandomAccess}. * * <p>This method also provides a convenient way to create a fixed-size * list initialized to contain several elements: * <pre> * List<String> stooges = Arrays.asList("Larry", "Moe", "Curly"); * </pre> * * @param a the array by which the list will be backed * @return a list view of the specified array */ @SafeVarargs public static <T> List<T> asList(T... a) { return new ArrayList<>(a); }