Java to improve the array of articles (2)

The last article defined arrays and briefly introduced arrays and one-dimensional arrays and two-dimensional arrays. We know that arrays are used to store data. When it comes to data, there must be operations on data, that is, adding, deleting, modifying, and checking, especially when it comes to When it comes to the array, the design is related to the data structure and algorithm. The content of the algorithm will be described in detail in the data structure and algorithm series blog. This blog introduces related methods based on the Array class, that is, counting the maximum, minimum, and minimum values ​​of the array. Implementation of average, sum, copy array, array comparison, array element comparison, array sorting, search, etc.

(1) Introduction to the Array class

The java.util.Arrays class is the tool class for manipulating arrays, including various methods for manipulating arrays (such as sorting and searching). It has a set of static methods for arrays,

  • This class contains various methods for manipulating arrays (such as sorting and searching). This class also contains a static factory, which can treat arrays as lists.
  • If the specified array reference is empty, all methods in this class throw one NullPointerException, unless otherwise stated.

(2) Count the maximum, minimum, average, and sum of the array 

package arrays;//: arrays/ArrayOptions.java
// Initialization & re-assignment of arrays.
class ArrayOption {
  public static void main(String args[]) {
    int data[] = new int[] {1, 2, 3, 4, 66, 5, 6, 7, 8, 9};
    double result[] = stat(data);
    double max =result[0];
    double min=result[1];
    double sum=result[2];
    double arr=result[3];
    System.out.println("最大值:" + max);
    System.out.println("最小值:" + min);
    System.out.println("数据总和:" + sum);
    System.out.println("平均值:" + arr);
  }

  //此时需要返回的数据一共有四个,那么一个方法只能够返回一种数据类型,所以应该使用数组返回
  //数组[0]为最大值、数组[1]为最小值、数组[2]为数据总和、数组[3]为平均值
  public static double[] stat(int data[]) {
    double reData[] = new double[4];
    reData[0] = data[0];
    reData[1] = data[0];
    reData[2] = data[0];
    for(int x = 1; x < data.length; x++) {
      reData[2] += data[x];
      if(data[x] > reData[0])
        reData[0] = data[x];
      if(data[x] < reData[1])
        reData[1] = data[x];
    }
    reData[3] = reData[2] / data.length;
    return reData;
  }
}

The results of the operation are:

(Three) copy the array

System.arraycopy() is used to copy the array. The parameters required by the arraycopy() method are: the original array, which represents the offset from where to start copying from the original array, and the offset from where to start copying from the target array, And the number of elements to be copied. Of course, any out-of-bounds operations on the array will cause exceptions.

package arrays;//: arrays/CopyingArrays.java
// Using System.arraycopy()
import java.util.*;

public class CopyingArrays {
  public static void main(String[] args) {
    int[] i = new int[7];
    int[] j = new int[10];
    Arrays.fill(i, 47);
    Arrays.fill(j, 99);
    System.out.println("i = " + Arrays.toString(i));
    System.out.println("j = " + Arrays.toString(j));
    System.arraycopy(i, 0, j, 0, i.length);
    System.out.println("j = " + Arrays.toString(j));
    int[] k = new int[5];
    Arrays.fill(k, 103);
    System.arraycopy(i, 0, k, 0, k.length);
    System.out.println("k = " + Arrays.toString(k));
    Arrays.fill(k, 103);
    System.arraycopy(k, 0, i, 0, k.length);
    System.out.println("i = " + Arrays.toString(i));
    // Objects:
    Integer[] u = new Integer[10];
    Integer[] v = new Integer[5];
    Arrays.fill(u, new Integer(47));
    Arrays.fill(v, new Integer(99));
    System.out.println("u = " + Arrays.toString(u));
    System.out.println("v = " + Arrays.toString(v));
    System.arraycopy(v, 0, u, u.length/2, v.length);
    System.out.println("u = " + Arrays.toString(u));
  }
} /* Output:
i = [47, 47, 47, 47, 47, 47, 47]
j = [99, 99, 99, 99, 99, 99, 99, 99, 99, 99]
j = [47, 47, 47, 47, 47, 47, 47, 99, 99, 99]
k = [47, 47, 47, 47, 47]
i = [103, 103, 103, 103, 103, 47, 47]
u = [47, 47, 47, 47, 47, 47, 47, 47, 47, 47]
v = [99, 99, 99, 99, 99]
u = [47, 47, 47, 47, 47, 99, 99, 99, 99, 99]
*///:~

The results of the operation are:

(4) Array comparison

The condition for the arrays to be equal is that the number of elements must be equal, and the elements at the corresponding positions are also equal.

package arrays;//: arrays/ComparingArrays.java
// Using Arrays.equals()
import java.util.*;


public class ComparingArrays {
  public static void main(String[] args) {
    int[] a1 = new int[10];
    int[] a2 = new int[10];
    Arrays.fill(a1, 47);
    Arrays.fill(a2, 47);
    System.out.println(Arrays.equals(a1, a2));
    a2[3] = 11;
    System.out.println(Arrays.equals(a1, a2));
    String[] s1 = new String[4];
    Arrays.fill(s1, "Hi");
    String[] s2 = { new String("Hi"), new String("Hi"),
      new String("Hi"), new String("Hi") };
    System.out.println(Arrays.equals(s1, s2));
  }
} /* Output:
true
false
true
*///:~

The results of the operation are:

 

(5) Comparison of array elements

 package arrays;//: arrays/CompType.java
// Implementing Comparable in a class.


import java.util.Arrays;

public class CompType  {

  public static void main(String[] args) {

    int []a=new int[]{11,22,55,99,452,65,1,5,486,22 };
    System.out.println("before sorting:");
    System.out.println(Arrays.toString(a));
    Arrays.sort(a);
    System.out.println("after sorting:");
    System.out.println(Arrays.toString(a));
  }
}

 

The results of the operation are:

(6) Sorting the array

package arrays;//: arrays/StringSorting.java

import java.util.Arrays;
import java.util.Collections;

public class StringSorting {
  public static void main(String[] args) {

    String [] sa=new String[]{"YNzbr", "nyGcF", "OWZnT", "cQrGs", "eGZMm", "JMRoE", "suEcU", "OneOE"
            , "dLsmw", "HLGEa", "hKcxr", "EqUCB", "bkIna", "Mesbt", "WHkjU", "rUkZP", "gwsqP", "zDyCy", "RFJQA", "HxxHv"};
    System.out.println("Before sort: " + Arrays.toString(sa));
    Arrays.sort(sa);
    System.out.println("After sort: " + Arrays.toString(sa));
    Arrays.sort(sa, Collections.reverseOrder());
    System.out.println("Reverse sort: " + Arrays.toString(sa));
    Arrays.sort(sa, String.CASE_INSENSITIVE_ORDER);
    System.out.println("Case-insensitive sort: " + Arrays.toString(sa));
  }
} /* Output:
Before sort: [YNzbr, nyGcF, OWZnT, cQrGs, eGZMm, JMRoE, suEcU, OneOE, dLsmw, HLGEa, hKcxr, EqUCB, bkIna, Mesbt, WHkjU, rUkZP, gwsqP, zDyCy, RFJQA, HxxHv]
After sort: [EqUCB, HLGEa, HxxHv, JMRoE, Mesbt, OWZnT, OneOE, RFJQA, WHkjU, YNzbr, bkIna, cQrGs, dLsmw, eGZMm, gwsqP, hKcxr, nyGcF, rUkZP, suEcU, zDyCy]
Reverse sort: [zDyCy, suEcU, rUkZP, nyGcF, hKcxr, gwsqP, eGZMm, dLsmw, cQrGs, bkIna, YNzbr, WHkjU, RFJQA, OneOE, OWZnT, Mesbt, JMRoE, HxxHv, HLGEa, EqUCB]
Case-insensitive sort: [bkIna, cQrGs, dLsmw, eGZMm, EqUCB, gwsqP, hKcxr, HLGEa, HxxHv, JMRoE, Mesbt, nyGcF, OneOE, OWZnT, RFJQA, rUkZP, suEcU, WHkjU, YNzbr, zDyCy]
*///:~

The results of the operation are:
 

 
Before sort: [YNzbr, nyGcF, OWZnT, cQrGs, eGZMm, JMRoE, suEcU, OneOE, dLsmw, HLGEa, hKcxr, EqUCB, bkIna, Mesbt, WHkjU, rUkZP, gwsqP, zDyCy, RFJQA, HxxHv]
After sort: [EqUCB, HLGEa, HxxHv, JMRoE, Mesbt, OWZnT, OneOE, RFJQA, WHkjU, YNzbr, bkIna, cQrGs, dLsmw, eGZMm, gwsqP, hKcxr, nyGcF, rUkZP, suEcU, zDyCy]
Reverse sort: [zDyCy, suEcU, rUkZP, nyGcF, hKcxr, gwsqP, eGZMm, dLsmw, cQrGs, bkIna, YNzbr, WHkjU, RFJQA, OneOE, OWZnT, Mesbt, JMRoE, HxxHv, HLGEa, EqUCB]
Case-insensitive sort: [bkIna, cQrGs, dLsmw, eGZMm, EqUCB, gwsqP, hKcxr, HLGEa, HxxHv, JMRoE, Mesbt, nyGcF, OneOE, OWZnT, RFJQA, rUkZP, suEcU, WHkjU, YNzbr, zDyCy]

Process finished with exit code 0

 

(7) Find

package arrays;//: arrays/ArraySearching.java
// Using Arrays.binarySearch().
import java.util.*;
public class ArraySearching {
  public static void main(String[] args) {
    int []a=new int[]{11,22,55,99,452,65,1,5,486,22 };
    System.out.println("Sorted before: " + Arrays.toString(a));
    Arrays.sort(a);
    System.out.println("Sorted array: " + Arrays.toString(a));
    System.out.println("数组中22 出现的位置:"+Arrays.binarySearch(a, 22));
  }
}

The results of the operation are:

(8) Return an array

package arrays;//: arrays/IceCream.java
// Returning arrays from methods.
import java.util.*;

public class IceCream {
  private static Random rand = new Random(47);
  static final String[] FLAVORS = {
    "Chocolate", "Strawberry", "Vanilla Fudge Swirl",
    "Mint Chip", "Mocha Almond Fudge", "Rum Raisin",
    "Praline Cream", "Mud Pie"
  };
  public static String[] flavorSet(int n) {
    if(n > FLAVORS.length)
      throw new IllegalArgumentException("Set too big");
    String[] results = new String[n];
    boolean[] picked = new boolean[FLAVORS.length];
    for(int i = 0; i < n; i++) {
      int t;
      do
        t = rand.nextInt(FLAVORS.length);
      while(picked[t]);
      results[i] = FLAVORS[t];
      picked[t] = true;
    }
    return results;
  }
  public static void main(String[] args) {
    for(int i = 0; i < 7; i++)
      System.out.println(Arrays.toString(flavorSet(3)));
  }
} /* Output:
[Rum Raisin, Mint Chip, Mocha Almond Fudge]
[Chocolate, Strawberry, Mocha Almond Fudge]
[Strawberry, Mint Chip, Mocha Almond Fudge]
[Rum Raisin, Vanilla Fudge Swirl, Mud Pie]
[Vanilla Fudge Swirl, Chocolate, Mocha Almond Fudge]
[Praline Cream, Strawberry, Mocha Almond Fudge]
[Mocha Almond Fudge, Strawberry, Mint Chip]
*///:~

The results of the operation are:

The above codes are from Java programming ideas.

Guess you like

Origin blog.csdn.net/weixin_41792162/article/details/109709320