[ Published on -
1. Java - Primitives
2. Java - Strings
3. Java - Arrays
4. Java - Date
5. Java - Generics
6. Java - Collections
7. Java - Localization
8. Java - Formatting
9. Java - Regular Expressions
10. Java - System
11. Java - Serialization
12. Java - Multithreading
13. Java - IO/File
14. Java - Networking
15. Java - ORM - JPA
a) Simple User entity class
package basics.basicsExamples; import java.util.Arrays; import java.util.Collections; import java.util.List; import basics.collectionsExamples.comparators.IntegerComparator; /** **************************************************************************** * ARRAY EXAMPLE CLASS * @author stanislav.zorjan */ public class ArrayExample { private byte [] byteArray0 = {1, 2, 4, 5}; private byte [] byteArray1 = new byte[10]; private byte [] byteArray2 = "This is a string".getBytes(); private Byte [] byteArray3 = new Byte[]{1,2,3,4,5}; private Byte [][] byteArray4 = new Byte[][]{{1,3,4},{1,2,3}}; private short [] shortArray0 = {1,2,3,4,5}; private short [] shortArray1 = new short[10]; private Short [] shortArray2 = new Short[]{1,2,3,4,5}; private int [] intArray0 = {1,2,3,4,5}; private int [] intArray1 = new int[10]; private Integer [] intArray2 = new Integer[]{1,2,3,4,5}; private Integer [] intArray3 = new Integer[]{2,2,3,4,5}; private long [] longArray0 = {1, 2L, 3l, 3000000000L, 5000000000L}; private long [] longArray1 = new long[10]; private Long [] longArray2 = new Long[]{1L, 2L, 3l, 3000000000L, 5000000000L}; private float [] floatArray0 = {1, 123, 12.12F, 123421.3F}; private float [] floatArray1 = new float[10]; private Float [] floatArray2 = new Float[]{1F, 123F, 12.12F, 123421.3F}; private double [] doubleArray0 = {1, 123D, 1123.12D, 12312.123}; private double [] doubleArray1 = new double[10]; private Double [] doubleArray2 = new Double[]{1D, 123D, 1123.12D, 12312.123}; private boolean [] booleanArray0 = {true, false, true, true, false}; private boolean [] booleanArray1 = new boolean[10]; private Boolean [] booleanArray2 = new Boolean[]{true, false, true, true, false}; private char [] charArray0 = {'c', 'd', '3', '\u00AE'}; private char [] charArray1 = new char[10]; private Character [] charArray2 = new Character[]{'c', 'd', '3', '\u00AE'}; private String [] str0 = {"0", "1", "2", "3"}; private String [] str1 = new String[10]; private String [] str2 = new String[]{"1", "2", "3", "4"}; /** ************************************************************************ * Default constructor */ public ArrayExample() { //printArrays(); arrayOperations(); } /** ************************************************************************ * Printing arrays */ private void printArrays(){ System.out.println(""); System.out.println("byte array"); System.out.println(Arrays.toString(this.byteArray0)); System.out.println(Arrays.toString(this.byteArray1)); System.out.println(Arrays.toString(this.byteArray2)); System.out.println(""); System.out.println("short array"); System.out.println(Arrays.toString(this.shortArray0)); System.out.println(Arrays.toString(this.shortArray1)); System.out.println(""); System.out.println("int array"); System.out.println(Arrays.toString(this.intArray0)); System.out.println(Arrays.toString(this.intArray1)); System.out.println(""); System.out.println("long array"); System.out.println(Arrays.toString(this.longArray0)); System.out.println(Arrays.toString(this.longArray1)); System.out.println(""); System.out.println("float array"); System.out.println(Arrays.toString(this.floatArray0)); System.out.println(Arrays.toString(this.floatArray1)); System.out.println(""); System.out.println("double array"); System.out.println(Arrays.toString(this.doubleArray0)); System.out.println(Arrays.toString(this.doubleArray1)); System.out.println(""); System.out.println("boolean array"); System.out.println(Arrays.toString(this.booleanArray0)); System.out.println(Arrays.toString(this.booleanArray1)); System.out.println(""); System.out.println("char array"); System.out.println(Arrays.toString(this.charArray0)); System.out.println(Arrays.toString(this.charArray1)); } /** ************************************************************************ * Array operations method */ private void arrayOperations(){ // converting array to List // For List interface see "collections.CollectionsExample" class // if you need to convert array to list by using Arrays.toList method, // than create "wrapped/boxed" array (Integer, Character, Float...), // otherwise if used primitive type, array values won't be copied to list // but instead, the whole array without conversion to list will // become the list element. List list = Arrays.asList(this.byteArray3); System.out.println(list); // searching array using binarySearch algorithm System.out.println(Arrays.binarySearch(intArray2, 4)); /* searching range of array using binarySearch algorithm * if fromIndex is > than searched object, result will * be number less than 0 * * For example: Integer [] intArray = new int[]{1,2,3,4,5}; * Arrays.binarySearch(intArray, 3, 4, 3); * will return -4 or -(fromIndex+1) */ /* * @param arrayToSearch * @param fromIndex * @param toIndex * @searchedValue */ System.out.println(Arrays.binarySearch(intArray2, 0, 4, 2)); // creating new array by copying array System.out.println(Arrays.toString(Arrays.copyOf(intArray0, 2))); // creating new array by copying only a particular range of array System.out.println(Arrays.toString(Arrays.copyOfRange(intArray0, 2, 4))); // compares two non primitive arrays (Integer, Double, Character...) System.out.println(Arrays.equals(intArray2, intArray3)); System.out.println(Arrays.deepEquals(intArray2, intArray3)); // Arrays hash code System.out.println(Arrays.deepHashCode(intArray2)); // prints array with all nested arrays System.out.println(Arrays.deepToString(this.byteArray4)); // fills all array positions with value Arrays.fill(intArray2, 10); System.out.println(Arrays.toString(intArray2)); // fills array positions within specified range Arrays.fill(intArray2, 1, 4, 4); System.out.println(Arrays.toString(intArray2)); // sorting in natural ascending order Arrays.sort(intArray2); System.out.println(Arrays.toString(intArray2)); // sorting array using comparator Arrays.sort(intArray2, new IntegerComparator()); System.out.println(Arrays.toString(intArray2)); // sorting only specified range of the array using comparator Arrays.sort(intArray2, 2, 4, new IntegerComparator()); System.out.println(Arrays.toString(intArray2)); // shuffling array Collections.shuffle(Arrays.asList(intArray2)); System.out.println(Arrays.asList(intArray2)); System.out.println(Arrays.toString(intArray2)); } }