The output of an array of Java on time, not directly output
System.out.println(arr);
Name directly output data is output memory address of the array. In other words, he is the output:
getClass().getName() + '@' + Integer.toHexString(hashCode())
toString () is a method of the Object class, so the default java in all classes inherit this, so by default all objects have this method.
It is usually just for the convenience of output, such as System.out.println (xx), inside the brackets "xx" if it is not of type String, then it automatically call xx's toString () method
When we direct the output array name, calls the toString (), then "[I @ 53d3d20b" This data is output, so when we want to be in accordance with our ideas, do not want to use a for loop to print this array we can be realized by rewriting the array toString () method we want.
1 @Override 2 public String toString() { 3 StringBuilder res = new StringBuilder(); 4 res.append('['); 5 for (int i = 0; i < size; i++) { 6 res.append(data[i]); 7 if (i != size - 1) 8 res.append(","); 9 } 10 res.append(']'); 11 return res.toString(); 12 }
Of course, I want to use this method need to rewrite the code for the entire array, so we can write a generic code array:
package Array; import java.util.Objects; public class Array<E> { private E[] data; private int size; // constructor, passing the array capacity public the Array ( int Capacity) { // Data new new = E [Capacity]; Data = (E []) new new Object [Capacity]; // casts size = 0 ; } public Array() { this(10); } public int getSize() { return size; } public int getCap() { return data.length; } public void addLast(E e) { add(size, e); } public void addfirst (E) { add(0, e); } // contains elements E public Boolean the contains (E E) { for ( int I = 0; I <size; I ++ ) { // if (data [i] == E) // reference comparison IF (Data [I] .equals (E)) // value comparison return to true ; } return false; } public int find(E e) { for (int i = 0; i < size; i++) { if (data[i].equals(e)) return i; } return -1; } public void add(int index, E e) { if (index < 0 || index > size) throw new IllegalArgumentException("Add failed.index is not support."); if (size == data.length) resize(2 * data.length); for (int i = size - 1; i >= index; i--) data[i + 1] = data[i]; data[index] = e; size++; } // get the index position of the index element E GET ( int index) { IF (index <0 || index> = size) { the throw new new an IllegalArgumentException ( "Not Support index IS!" ); } return data[index]; } void set(int index, E e) { if (index < 0 || index >= size) { throw new IllegalArgumentException("index is not support!"); } data[index] = e; } // delete the index position of the element, and returns the removed element of what public E the Remove ( int index) { IF (index <0 || index> = size) { the throw new new IllegalArgumentException ( "Support index IS not!" ); } E ret = data[index]; for (int i = index + 1; i < size; i++) { data[i - 1] = data[i]; } size--; if (size == data.length/2) resize(data.length/2); return ret; } public E removeFirst() { return remove(0); } public E removeLast() { return remove(size - 1); } // removed from the array element E public Boolean removeElement (E E) { int index = Find (E); IF (index = -1! ) { remove(index); return true; } else { return false; } } public void removeElementAll(int e) { } public int findAll(int e) { return -1; } @Override public String toString() { StringBuilder res = new StringBuilder(); res.append(String.format("Array:size= %d,cap = %d\n", size, data.length)); res.append('['); for (int i = 0; i < size; i++) { res.append(data[i]); if (i != size - 1) res.append(","); } res.append(']'); return res.toString(); } private void resize(int newCapacity) { E[] newData = (E[]) new Object[newCapacity]; for (int i = 0; i < size; i++) { newData[i] = data[i]; } data = newData; System.out.println ( "the dynamic expansion!" ); } }