JavaRush /Java Blog /Random EN /10 Frequently Used Array Tricks in Java
theGrass
Level 24
Саратов

10 Frequently Used Array Tricks in Java

Published in the Random EN group
The following 10 tricks for arrays in Java are commonly used. They are the highest rated on Stack Overflow, a programming question and answer system. 10 Frequently Used Array Techniques in Java - 1
  1. Array declaration

    String[] aArray = new String[5];
    String[] bArray = {"a","b","c", "d", "e"};
    String[] cArray = new String[]{"a","b","c","d","e"};
  2. Array Output in Java

    int[] intArray = { 1, 2, 3, 4, 5 };
    String intArrayString = Arrays.toString(intArray);
    
    // print directly will print reference value
    System.out.println(intArray);
    // [I@7150bd4d
    
    System.out.println(intArrayString);
    // [1, 2, 3, 4, 5]
  3. Creating an ArrayList from an Array

    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]
  4. Checking an array for a specific value

    String[] stringArray = { "a", "b", "c", "d", "e" };
    boolean b = Arrays.asList(stringArray).contains("a");
    System.out.println(b);
    // true
  5. Union of two arrays

    int[] intArray = { 1, 2, 3, 4, 5 };
    int[] intArray2 = { 6, 7, 8, 9, 10 };
    // Apache Commons Lang library
    int[] combinedIntArray = ArrayUtils.addAll(intArray, intArray2);
  6. Array declaration in one line

    method(new String[]{"a", "b", "c", "d", "e"});
  7. Combining array elements into a string

    // 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
  8. Convert ArrayListto array

    String[] stringArray = { "a", "b", "c", "d", "e" };
    ArrayList<String> arrayList = new ArrayList<String>(Arrays.asList(stringArray));
    String[] stringArr = new String[arrayList.size()];
    arrayList.toArray(stringArr);
    for (String s : stringArr)
    	System.out.println(s);
  9. Convert array to set ( Set)

    Set<String> set = new HashSet<String>(Arrays.asList(stringArray));
    System.out.println(set);
    //[d, e, b, c, a]
  10. Returning an array with elements in reverse order

    int[] intArray = { 1, 2, 3, 4, 5 };
    ArrayUtils.reverse(intArray);
    System.out.println(Arrays.toString(intArray));
    //[5, 4, 3, 2, 1]
  11. 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));

    And one more thing - creating a type array bytebased on the type value int(note we take ByteBuffer, select 4 bytes in it and put the number int8, then we will convert all this (0, 0, 0, 8) into a type array byteusing the call array())

    byte[] bytes = ByteBuffer.allocate(4).putInt(8).array();
    
    for (byte t : bytes) {
       System.out.format("0x%x ", t);
    }
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION