1. Define a Java array
1 New String[5]; 2 string[] Barray = {"A", "B", "C", "D", "E"}; 3 New String[]{"A", "B", "C", "D", "E"};
The first is to define an array and specify the length of the array, which we call the dynamic definition.
The second and third types also initialize values while allocating memory space.
2. Printing elements in a Java array
int [] Intarray = {1, 2, 3, 4, 5= arrays.tostring (intarray); // Print directly would print reference value System.out.println (intarray); // [email protected]System.out.println (intarraystring); // [1, 2, 3, 4, 5]
The focus here is to illustrate the reference and merit of the array in Java, the third line directly prints Intarray, the output is garbled, because Intarray is only an address reference. The 4th line of output is the true array value, as it passes through the arrays.tostring () conversion. For Java beginners, references and values still need to be valued.
3. Create a ArrayList from the array
String[] Stringarray = {"A", "B", "C", "D", "E" }; ArrayListNew arraylist<string>(arrays.aslist (Stringarray)); System.out.println (arrayList); // [A, B, C, D, E]
Why convert an array to ArrayList? Perhaps because ArrayList is a dynamic linked list, we can more easily add and remove ArrayList, we do not need to loop array to each element added to the ArrayList, with the above code can be easily converted.
4. Check if the array contains a value
String[] Stringarray = {"A", "B", "C", "D", "E" }; Boolean b = arrays.aslist (Stringarray). Contains ("a"); System.out.println (b); // true
Use Arrays.aslist () first to convert the array to List<string>, so you can use the contains function of the dynamic list to determine whether the element is contained in the linked list.
5. Connect two arrays
int [] Intarray = {1, 2, 3, 4, 5 }; int [] IntArray2 = {6, 7, 8, 9, ten }; // Apache Commons Lang Library int [] Combinedintarray = Arrayutils.addall (Intarray, intArray2);
Arrayutils is an array processing class library provided by Apache, and its AddAll method makes it easy to concatenate two arrays into an array.
6. Declare an array within the chain (?)
Method (new string[]{"A", "B", "C", "D", "E"});
7. The elements in the array are exported as strings
// containing the provided list of elements // Apache Common Lang String j = stringutils.join (new string[] {"A", "B", "C"}, ","); System.out.println (j); // A, B, C
Also, using the Join method in StringUtils, you can output the elements in the array as a string.
8. Converting an array into a set set
New Hashset<string>(Arrays.aslist (Stringarray)); SYSTEM.OUT.PRINTLN (set); // [D, E, B, C, a]
Using set in Java makes it easy to save the required types in a variable in a collection type, primarily in a display list. You can also convert an array to a list before converting the list to set.
Difference between Set and HashSet:
A Set represents a generic "set of values". A TreeSet is a set where the elements be sorted (and thus ordered), A is HashSet a set where the elements is rted or ordered.
A is HashSet typically a lot faster than a TreeSet .
9. Array flipping
int [] Intarray = {1, 2, 3, 4, 5 }; Arrayutils.reverse (Intarray); System.out.println (arrays.tostring (Intarray)); // [5, 4, 3, 2, 1]
Still use the omnipotent arrayutils.
10. Removing an element from an array
int [] Intarray = {1, 2, 3, 4, 5 }; int [] removed = Arrayutils.removeelement (Intarray, 3); // Create a new array System.out.println (arrays.tostring (removed));
Add one: Convert an int value to a byte array
byte [] bytes = Bytebuffer.allocate (4). Putint (8). Array (); for (byte t:bytes) {System.out.format ("0x%x", T);}
Reference:
Http://www.codeceo.com/article/10-java-array-method.html
Http://stackoverflow.com/questions/5139724/whats-the-difference-between-hashset-and-set
Array: Common Java Operations