This article has participated in the call for good writing activities, click to view: back end, big front end double track submission, 20,000 yuan prize pool waiting for you to challenge!

Like it and see. Make it a habit

directory

1. Array definition

2. Array traversal

3, List and array conversion

4. Possible problems

1. Array definition

Speak first and use later

Data type [] Array name = new data type [length]; String[] arr3 = new String[5];

Data type array name [] = new data type [length]; String arr[] = new String[5];

Direct initialization

String[] arrs = {“1″,”2″,”3”};

Declare and initialize

String[] sarr = new String[]{“a”,”b”};

2. Array traversal

Flow traversal

Encapsulate arrays as streams to operate on, and all operations operate like lists

public static void main(String[] args) {
      String[] arrs = {"1"."2"."3"};
      Arrays.stream(arrs).forEach(System.out::println);
  }
Copy the code

Ordinary traversal

There are three common traversal methods. The first one is recommended.

You can use the third one if you want to use an index

If you want reverse order, use the third

3, List and array conversion

List and array are closely related. List is implemented in linked lists and arrays, and we often need to convert them in development

Array to list

Use a loop to convert to list

By tool method

Code display:

  public static void main(String[] args) {
       String[]  arrs = {"1"."2"."3"};
       // Loop conversion
       List<String> list1 = new ArrayList<>();
       for (String arr : arrs) {
           list1.add(arr);
      }
       // Use the array utility class
       List<String> list2 = Arrays.asList(arrs);
       // Use the collection utility class
       List<String> list3 = new ArrayList<>();
       Collections.addAll(list2,arrs);
  }
Copy the code

2, list to array

List built-in method toArray

Direct cycle

The code shown

public static void main(String[] args) { List list = Arrays.asList(“1″,”2”);

String[] arr1 = new String[list.size()]; // loop over assignment for (int I = 0; i < list.size(); i++) { arr1[i] = list.get(i); } // Call list method String[] arr2 = (String[]) list.toarray ();Copy the code

}

4. Arrays utility classes

toString()

The method that prints an array, if not called, prints the memory address

stream()

I won’t show you how to convert an array to a stream

sort()

Sort the array. Note that this sort is an internal sort of the array. There is no return value

setAll

To operate on elements in an array, you need to provide a function with different data types

String[] arrs = {"1"."2"."3"};
      Arrays.setAll(arrs, e->e+"0");
      for (String arr : arrs) {
          System.out.println(arr);
      }
Copy the code

binarySearch

See the name all know this is binary search, the specific algorithm is also very simple, if you do not know, can make up for it, if you know binary search should know, before calling this method to ensure that the array is in order!

copyOf

The name is pretty straightforward, copying an array, extending is just adding some length limit, or populating the data Settings

equals

It seems that no explanation is needed

deepEquals

Determine whether the two arrays are of the same depth, i.e., how many layers are nested

public static void main(String[] args) {
      String[] arrs = {"1"."2"."3"};
      String[][] arr2 = {{"1"}, {"2"}, {"3"}};
      boolean b = Arrays.deepEquals(arr2, arrs);
      System.out.println(b);
  }
Copy the code

fill

Basically, if you know English, you know how to fill an array, but you can loop through it yourself, right

hashCode

Computes the hash code of the array

parallelPrefix

This is kind of interesting, parallel accumulative operations on the elements of an array, just to show you an example

public static void main(String[] args) {
       String[]  sarr = new String[]{"a"."b"."c"};
 
       Arrays.parallelPrefix(sarr, (sum,e1)->e1 + sum);
 
       System.out.println(Arrays.toString(sarr));
 
  }
Copy the code

Take a look at the execution result:

4. Possible problems

1. The index is out of bounds. The index of the array starts from 0 and the last index is length-1

2. The list created in the following way cannot be added

Because the ArrayList in Arrays doesn’t implement the remove() and add() methods, it throws exceptions. AsList returns a List of immutable length. This List no longer has many of the features of the original List, so be careful with arrays. asList.

String[] arr = {"1"."2"."3"};
       List list = Arrays.asList(arr);
       arr[1] = "4";
       try {
           list.add("5");
      } catch (Exception ex) {
           ex.printStackTrace();
      }
Copy the code

3. A trick to loop an array through

Use mod to the array length

   public static void main(String[] args) {
       String[] arr = {"a"."b"."c"};
       int i = 0;
       int j = 0;
       int length = arr.length;
       while (j++ <10){ System.out.println(arr[i%length]); i++; }}Copy the code

You can see that the array is iterated over multiple times