1. Define a Java array
string[] Aarray = new String[5];
String[] Barray = {"A", "B", "C", "D", "E"};
string[] CArray = 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. Print elements in Java array
Int[] Intarray = {1, 2, 3, 4, 5};
String intarraystring = arrays.tostring (intarray);
Print directly'll print reference value
System.out.println (intarray);
[I@7150bd4d
System.out.println (intarraystring);
[1, 2, 3, 4, 5]
Here the focus is to illustrate the Java array reference and worth distinguishing, the third line of direct printing intarray, output is garbled, because Intarray is only an address reference. Line 4th outputs the true array value, as it passes through the transformation of arrays.tostring (). For Java beginners, references and values still need to be valued.
3, from the array to create ArrayList
String[] Stringarray = {"A", "B", "C", "D", "E"};
arraylist<string> ArrayList = new Arraylist<string> (arrays.aslist (Stringarray));
System.out.println (arrayList);
[A, B, C, D, E]
Why do you want to convert the array to ArrayList? May be because ArrayList is a dynamic list, we can more easily add and remove ArrayList, we do not need to cycle array of each element into the ArrayList, with the above code can be simple to implement conversion.
4, check whether 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 () to convert an array to List<string>, so that you can use the contains function of a dynamic list to determine whether an element is contained in a 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 connect two arrays into an array.
6. Declare an array inner chain
Method (New string[]{"A", "B", "C", "D", "E"});
7, the elements of the array in the form of a string output
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, the elements in an array can be exported as a string.
8. Convert array to set set
set<string> set = new Hashset<string> (arrays.aslist (Stringarray));
SYSTEM.OUT.PRINTLN (set);
[D, E, B, C, a]
Using set in Java, you can easily save the type you want in a variable with a collection type, mainly in the display list. You can also convert the array to a list and then convert the list to set.
9. Array Flip
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. Remove an element from the 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 another: Converts an int value into a byte array
byte[] bytes = bytebuffer.allocate (4). Putint (8). Array ();
for (byte t:bytes) {
System.out.format ("0x%x", t);
}
Top ten Methods for Java Arrays
Translation Author: Code Rural network – Xiao Feng