Table of Contents
ToggleJava Arrays Class
介紹常見的 equals 、 toString 、 parallelPrefix 等方法,陣列類別工具,由靜態方法和物件類的方法組成,Arrays Class 支援陣列操作,本篇增加了範例,並透過單元測試來驗證產出結果。
檔案目錄
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- java_147
| +- array
| +- ArraysClassTest.java
單元測試
Java Arrays 陣列類別工具,提供比較、轉換字串、更新元素等操作陣列中的元素。
Fruit
建立 Fruit 類別,覆寫 equals 、 hashCode ,定義屬性和方法,用來建立一個物件。
@NoArgsConstructor
@Getter
@Setter
@Builder
public static class Fruit {
private String name;
private double quantity;
private int type;
public Fruit(String name, double quantity, int type) {
this.name = name;
this.quantity = quantity;
this.type = type;
}
public String toString() {
ToStringBuilder builder = new ToStringBuilder(this, ToStringStyle.JSON_STYLE);
builder.appendSuper(super.toString());
builder.append("name", name);
builder.append("quantity", quantity);
builder.append("type", type);
return builder.toString();
}
}
equals
建立二個 Array ,內有三個元素,比較是否相等。
@Test
public void equals() {
String[] array = new String[] { "Durian", "Guava", "Pitaya" };
String[] array2 = new String[] { "Durian", "Guava", "Pitaya" };
boolean result = Arrays.equals(array, array2);
System.out.println(result);
assertThat(result).isTrue();
String[] array3 = new String[] { "Mango" };
result = Arrays.equals(array, array3);
System.out.println(result);
assertThat(result).isFalse();
}
true
false
deepToEquals
Java Arrays 建立二個 Array ,內有三個元素,比較是否相等。
@Test
public void deepToEquals() {
Fruit durian = new Fruit("Durian", 1, 1);
Fruit pitaya = new Fruit("Guava", 2, 1);
Fruit guava = new Fruit("Pitaya", 3, 1);
Fruit[] array = new Fruit[] { durian, pitaya, guava };
Fruit[] array2 = new Fruit[] { durian, pitaya, guava };
boolean result = Arrays.equals(array, array2);
System.out.println(result);
assertThat(result).isTrue();
Fruit[] array3 = new Fruit[] { new Fruit("Durian", 1, 1), new Fruit("Guava", 2, 1), new Fruit("Pitaya", 3, 1) };
result = Arrays.equals(array, array3);
System.out.println(result);
assertThat(result).isFalse();
}
true
false
toStringz
Java Arrays 建立一個 Array ,內有三個元素,轉換成字串。
@Test
public void toStringz() {
String[] array = new String[] { "Durian", "Guava", "Pitaya" };
String result = Arrays.toString(array);
System.out.println(result);
assertThat(result).isEqualTo("[Durian, Guava, Pitaya]");
int[] intArray = new int[] { Integer.MAX_VALUE, -1, 3 };
String intResult = Arrays.toString(intArray);
System.out.println(intResult);
assertThat(intResult).isEqualTo("[2147483647, -1, 3]");
}
[Durian, Guava, Pitaya]
[2147483647, -1, 3]
deepToString
Arrays in Java 建立一個 Array ,內有三個元素,轉換成字串。
@Test
public void deepToString() {
Fruit durian = new Fruit("Durian", 1, 1);
Fruit pitaya = new Fruit("Guava", 2, 1);
Fruit guava = new Fruit("Pitaya", 3, 1);
Fruit[] array = new Fruit[] { durian, pitaya, guava };
String result = Arrays.deepToString(array);
System.out.println(result);
assertThat(result).isEqualTo(
"[{\"name\":\"Durian\",\"quantity\":1.0,\"type\":1}, {\"name\":\"Guava\",\"quantity\":2.0,\"type\":1}, {\"name\":\"Pitaya\",\"quantity\":3.0,\"type\":1}]");
}
[{"name":"Durian","quantity":1.0,"type":1}, {"name":"Guava","quantity":2.0,"type":1}, {"name":"Pitaya","quantity":3.0,"type":1}]
parallelPrefix
Arrays in Java 建立一個 Array ,內有三個元素,將目前索引位置的值與前一個索引的值進行操作,然後更新目前索引位置的值。
@Test
public void parallelPrefix() {
String[] array = new String[] { "Durian", "Guava", "Pitaya" };
Arrays.parallelPrefix(array, (e1, e2) -> {
return e1.toUpperCase() + "_" + e2;
});
System.out.println(Arrays.toString(array));
assertThat(array).containsExactly("Durian", "DURIAN_Guava", "DURIAN_GUAVA_Pitaya");
int[] intArray = new int[] { 1, 2, 3, 4, 5 };
Arrays.parallelPrefix(intArray, (e1, e2) -> e1 * e2);
System.out.println(Arrays.toString(intArray));
assertThat(intArray).containsExactly(1, 2, 6, 24, 120);
}
[Durian, DURIAN_Guava, DURIAN_GUAVA_Pitaya]
[1, 2, 6, 24, 120]
hashCodez
Arrays in Java 建立一個 Array ,內有三個元素,取得 hash code 。
@Test
public void hashCodez() {
String[] array = new String[] { "Durian", "Guava", "Pitaya" };
int result = Arrays.hashCode(array);
System.out.println(result);
assertThat(result).isEqualTo(-1680899964);
int[] intArray = new int[] { Integer.MAX_VALUE, -1, 3 };
int intResult = Arrays.hashCode(intArray);
System.out.println(intResult);
assertThat(intResult).isEqualTo(-2147454846);
}
-1680899964
-2147454846
ArraysClassTest.java
Arrays in Java 新增單元測試,驗證是否符合預期。
package org.ruoxue.java_147.array;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.Arrays;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.junit.Test;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
public class ArraysClassTest {
@NoArgsConstructor
@Getter
@Setter
@Builder
public static class Fruit {
private String name;
private double quantity;
private int type;
public Fruit(String name, double quantity, int type) {
this.name = name;
this.quantity = quantity;
this.type = type;
}
public String toString() {
ToStringBuilder builder = new ToStringBuilder(this, ToStringStyle.JSON_STYLE);
builder.appendSuper(super.toString());
builder.append("name", name);
builder.append("quantity", quantity);
builder.append("type", type);
return builder.toString();
}
}
@Test
public void equals() {
String[] array = new String[] { "Durian", "Guava", "Pitaya" };
String[] array2 = new String[] { "Durian", "Guava", "Pitaya" };
boolean result = Arrays.equals(array, array2);
System.out.println(result);
assertThat(result).isTrue();
String[] array3 = new String[] { "Mango" };
result = Arrays.equals(array, array3);
System.out.println(result);
assertThat(result).isFalse();
}
@Test
public void deepToEquals() {
Fruit durian = new Fruit("Durian", 1, 1);
Fruit pitaya = new Fruit("Guava", 2, 1);
Fruit guava = new Fruit("Pitaya", 3, 1);
Fruit[] array = new Fruit[] { durian, pitaya, guava };
Fruit[] array2 = new Fruit[] { durian, pitaya, guava };
boolean result = Arrays.equals(array, array2);
System.out.println(result);
assertThat(result).isTrue();
Fruit[] array3 = new Fruit[] { new Fruit("Durian", 1, 1), new Fruit("Guava", 2, 1), new Fruit("Pitaya", 3, 1) };
result = Arrays.equals(array, array3);
System.out.println(result);
assertThat(result).isFalse();
}
@Test
public void toStringz() {
String[] array = new String[] { "Durian", "Guava", "Pitaya" };
String result = Arrays.toString(array);
System.out.println(result);
assertThat(result).isEqualTo("[Durian, Guava, Pitaya]");
int[] intArray = new int[] { Integer.MAX_VALUE, -1, 3 };
String intResult = Arrays.toString(intArray);
System.out.println(intResult);
assertThat(intResult).isEqualTo("[2147483647, -1, 3]");
}
@Test
public void deepToString() {
Fruit durian = new Fruit("Durian", 1, 1);
Fruit pitaya = new Fruit("Guava", 2, 1);
Fruit guava = new Fruit("Pitaya", 3, 1);
Fruit[] array = new Fruit[] { durian, pitaya, guava };
String result = Arrays.deepToString(array);
System.out.println(result);
assertThat(result).isEqualTo(
"[{\"name\":\"Durian\",\"quantity\":1.0,\"type\":1}, {\"name\":\"Guava\",\"quantity\":2.0,\"type\":1}, {\"name\":\"Pitaya\",\"quantity\":3.0,\"type\":1}]");
}
@Test
public void parallelPrefix() {
String[] array = new String[] { "Durian", "Guava", "Pitaya" };
Arrays.parallelPrefix(array, (e1, e2) -> {
return e1.toUpperCase() + "_" + e2;
});
System.out.println(Arrays.toString(array));
assertThat(array).containsExactly("Durian", "DURIAN_Guava", "DURIAN_GUAVA_Pitaya");
int[] intArray = new int[] { 1, 2, 3, 4, 5 };
Arrays.parallelPrefix(intArray, (e1, e2) -> e1 * e2);
System.out.println(Arrays.toString(intArray));
assertThat(intArray).containsExactly(1, 2, 6, 24, 120);
}
@Test
public void hashCodez() {
String[] array = new String[] { "Durian", "Guava", "Pitaya" };
int result = Arrays.hashCode(array);
System.out.println(result);
assertThat(result).isEqualTo(-1680899964);
int[] intArray = new int[] { Integer.MAX_VALUE, -1, 3 };
int intResult = Arrays.hashCode(intArray);
System.out.println(intResult);
assertThat(intResult).isEqualTo(-2147454846);
}
}
心得分享
Java Arrays Example 支援陣列操作,比較、轉換字串、更新元素等方法, Arrays in Java 提供了幾種 Arrays 常見方法的操作範例,實現操作基本型別陣列及物件陣列簡便的方法。