Created
March 20, 2023 00:23
-
-
Save woopsla/469efe501b24202f581e1853b79afd86 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package cse2010.arrays; | |
import org.junit.jupiter.api.Test; | |
import java.util.Arrays; | |
import java.util.Comparator; | |
import java.util.stream.Collectors; | |
import static org.junit.jupiter.api.Assertions.*; | |
public class ArrayDemo2Test { | |
int as[] = { 1, 3, 5, 7, 9 }; | |
int bs[] = { 1, 3, 5, 7, 9 }; | |
@Test | |
void streamTest() { | |
int total = Arrays.stream(as).sum(); | |
assertEquals(25, total); | |
} | |
@Test | |
void arrayEquals_WrongWay() { | |
assertFalse(as.equals(bs)); | |
} | |
@Test | |
void arrayEquals_RightWay() { | |
assertTrue(Arrays.equals(as, bs)); | |
assertArrayEquals(as, bs); | |
} | |
@Test | |
void array_filling() { | |
int[] expected = { 777, 777, 777, 777, 777 }; | |
Arrays.fill(as, 777); | |
assertTrue(Arrays.equals(as, expected)); | |
} | |
@Test | |
void array_filling2() { | |
Arrays.fill(as, 1, 3, 0); | |
System.out.println(as.toString()); // maybe not what you want | |
System.out.println(Arrays.toString(as)); | |
} | |
@Test | |
void arrays_toString() { | |
for (int i = 0; i < as.length; i++) | |
System.out.println(as[i]); | |
// is equivalent to: (for-each loop) | |
for (int a : as) System.out.println(a); | |
System.out.println(Arrays.toString(as)); | |
} | |
@Test | |
void array_System_arraycopy() { | |
int[] as = { 2, 4, 6, 8, 10 }; | |
System.out.println(Arrays.toString(as)); | |
System.out.println(Arrays.toString(bs)); | |
System.arraycopy(as, 1, bs, 1, 3); | |
System.out.println(Arrays.toString(bs)); | |
} | |
@Test | |
void array_copyOf() { | |
int[] ys = Arrays.copyOf(as, 10); | |
System.out.println(Arrays.toString(ys)); | |
System.out.println(Arrays.toString(as)); | |
} | |
@Test | |
void array_copyOfRange() { | |
int ys[] = Arrays.copyOfRange(as, 1, 4); | |
System.out.println(Arrays.toString(ys)); | |
System.out.println(Arrays.toString(as)); | |
} | |
@Test | |
void arrays_setAll() { | |
int[] ints = new int[10]; | |
Arrays.setAll(ints, i -> i * i); | |
System.out.println(Arrays.toString(ints)); | |
} | |
@Test | |
void array_deepToString() { | |
int[][] two = { { 1, 2, 3 }, { 4, 5, 6 } }; | |
System.out.println(Arrays.toString(two)); // oops! | |
System.out.println(Arrays.deepToString(two)); | |
} | |
@Test | |
void array_sort() { | |
int xs[] = { 9, 7, 5, 3, 1 }; | |
Arrays.sort(xs); // quick sort | |
System.out.println(Arrays.toString(xs)); | |
} | |
private void reset(Integer[] xs) { | |
Arrays.setAll(xs, i -> i); | |
} | |
@Test | |
void array_sortWithComparator() { | |
Integer[] xs = new Integer[10]; | |
reset(xs); | |
System.out.println(Arrays.toString(xs)); | |
Arrays.sort(xs); | |
System.out.println(Arrays.toString(xs)); | |
reset(xs); | |
Arrays.sort(xs, (o1, o2) -> o1 - o2); // ascending order | |
System.out.println(Arrays.toString(xs)); | |
reset(xs); | |
Arrays.sort(xs, (o1, o2) -> o2 - o1); // descending order | |
System.out.println(Arrays.toString(xs)); | |
reset(xs); | |
Arrays.sort(xs, Comparator.naturalOrder()); | |
System.out.println(Arrays.toString(xs)); | |
reset(xs); | |
Arrays.sort(xs, Comparator.reverseOrder()); | |
System.out.println(Arrays.toString(xs)); | |
reset(xs); | |
Integer[] integers = Arrays.stream(xs).toArray(Integer[]::new); | |
System.out.println(Arrays.toString(integers)); | |
} | |
@Test | |
void miscellaneous1() { | |
int[][] as = new int[2][3]; | |
for (int i = 0; i < as.length; i++) { | |
final int k = i; | |
Arrays.setAll(as[i], j -> k + j); | |
} | |
System.out.println(Arrays.deepToString(as)); | |
int[][] bs = new int[2][]; | |
Arrays.setAll(bs, i -> new int[3]); | |
System.out.println(Arrays.deepToString(bs)); | |
} | |
@Test | |
void miscellaneous2() { | |
int[][] arrays = { { 1, 2, 3 }, { 4, 5, 6 } }; | |
Arrays.stream(arrays) | |
.forEach(e -> System.out.println(Arrays.toString(e))); | |
System.out.println("---------------------------"); | |
String values = Arrays.stream(arrays) | |
.map(Arrays::toString) | |
.collect(Collectors.joining("\n")); | |
System.out.println(values); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment