import java.util.Arrays;
int[] nums = {2, 4, 6, 8, 10};
System.out.println(Arrays.toString(nums));
[2, 4, 6, 8, 10]
int[] nums = {2, 4, 6, 8, 10};
String str = Arrays.toString(nums);
System.out.println(str.replaceAll("[\\[\\],]", ""));
2 4 6 8 10
String[] letters = {"A", "B", "C"};
System.out.println(Arrays.toString(letters));
[A, B, C]
public class Main {
public static void main(String[] args) {
Example[] arr = {new Example(), new Example(), new Example()};
System.out.println(Arrays.toString(arr));
}
}
class Example {
int num = 3;
}
[Example@3b22cdd0, Example@1e81f4dc, Example@4d591d15]
public static String valueOf(Object obj) {
return (obj == null) ? "null" : obj.toString();
}
class Example {
int num = 3;
public String toString() {
return "" + num;
}
}
[3, 3, 3]
System.out.println(arr.toString());
[LExample;@3d075dc0
int[] srcArr = {1, 2, 3, 4, 5};
int[] destArr = new int[srcArr.length];
// процесс копирования
for(int i = 0; i < destArr.length; i++) {
destArr[i] = srcArr[i];
}
int[] srcArr = {1, 2, 3, 4, 5};
int[] destArr = Arrays.copyOf(srcArr, srcArr.length);
int[] srcArr = {1, 2, 3, 4, 5};
int[] destArr = Arrays.copyOf(srcArr, srcArr.length + 3);
System.out.println(Arrays.toString(destArr));
destArr[srcArr.length] = 6;
destArr[srcArr.length + 1] = 7;
System.out.println(Arrays.toString(destArr));
[1, 2, 3, 4, 5, 0, 0, 0]
[1, 2, 3, 4, 5, 6, 7, 0]
String[] letters = {"A", "B", "C"};
int len = letters.length;
int newLen = len + (int) (len * 0.75);
System.out.println("Длина нового массива = " + newLen);
String[] destArr = Arrays.copyOf(letters, newLen);
System.out.println(Arrays.toString(destArr));
destArr[len] = "D";
System.out.println(Arrays.toString(destArr));
Длина нового массива = 5
[A, B, C, null, null]
[A, B, C, D, null]
int[] srcArr = {1, 2, 3, 4, 5};
int[] destArr = Arrays.copyOfRange(srcArr, 1, 3);
System.out.println(Arrays.toString(destArr));
[2, 3]
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
double[] srcArr = {100.1, 120.2, -4.8, 3.5, 70.2};
double max = srcArr[0];
int maxNumIndex = 0;
for (int i = 1; i < srcArr.length; i++) {
if (srcArr[i] > max) {
maxNumIndex = i;
max = srcArr[i];
}
}
double[] destArr = Arrays.copyOfRange(srcArr, maxNumIndex, srcArr.length);
System.out.println(Arrays.toString(destArr));
}
}
[120.2, -4.8, 3.5, 70.2]
@IntrinsicCandidate
public static native void arraycopy(Object src, int srcPos,
Object dest, int destPos,
int length)
import java.util.Arrays;
class Main {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
System.arraycopy(arr, 1, arr, 0, arr.length - 1);
System.out.println(Arrays.toString(arr));
arr[arr.length - 1] = 0;
System.out.println(Arrays.toString(arr));
}
}
[2, 3, 4, 5, 5]
[2, 3, 4, 5, 0]
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
Player[] srcArr = {new Player("Max"), new Player("Fox"), new Player("LoL")};
Player[] destArr = new Player[srcArr.length];
System.out.println("До копирования:\n" +
Arrays.toString(srcArr) + "\n" +
Arrays.toString(destArr));
System.arraycopy(srcArr, 0, destArr, 0, srcArr.length);
srcArr[0].setName(null);
System.out.println("\nПосле копирования:\n" +
Arrays.toString(srcArr) + "\n" +
Arrays.toString(destArr));
}
}
class Player {
private String name;
Player(String name) {
this.name = name;
}
public void setName(String name) {
this.name = name;
}
public String toString() {
return name;
}
}
System.arraycopy(original, 0, copy, 0,
Math.min(original.length, newLength));
int[] nums = new int[4];
Arrays.fill(nums, 36);
System.out.println(Arrays.toString(nums));
[36, 36, 36, 36]
String[] arr = new String[10];
Arrays.fill(arr, 3, 7, "Java");
System.out.println(Arrays.toString(arr));
[null, null, null, Java, Java, Java, Java, null, null, null]
Example[] arr = {new Example(), new Example(), new Example(), null, null};
Arrays.fill(arr, 0, 3, null);
System.out.println(Arrays.toString(arr));
[null, null, null, null, null]
char[] chars1 = {'M', 'A', 'X'};
char[] chars2 = {'M', 'A', 'N'};
boolean equals = true;
for (int i = 0; i < chars2.length; i++) {
if (chars1[i] != chars2[i]) {
equals = false;
break;
}
}
System.out.println("Массивы " + (equals ? "равны" : "не равны"));
Массивы не равны
public boolean equals(Object obj) {
return (this == obj);
}
double[] nums = {.5, .7, .3};
double[] copyNums = Arrays.copyOf(nums, nums.length);
System.out.println(Arrays.toString(nums));
System.out.println(Arrays.toString(copyNums));
System.out.println("Массивы " + (copyNums.equals(nums) ? "равны" : "не равны"));
[0.5, 0.7, 0.3]
[0.5, 0.7, 0.3]
Массивы не равны
boolean[] states1 = {false, true, false};
boolean[] states2 = {false, true, false};
boolean[] states3 = {false, false, true};
System.out.println("Массивы " + (Arrays.equals(states1, states2) ? "равны" : "не равны"));
System.out.println("Массивы " + (Arrays.equals(states1, states3) ? "равны" : "не равны"));
Массивы равны
Массивы не равны
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
Example[] arr1 = {new Example(), new Example()};
Example[] arr2 = {new Example(), new Example()};
System.out.println(Arrays.equals(arr1, arr2));
}
}
class Example {
int num = 3;
public String toString() {
return "" + num;
}
}
class Example {
int num = 3;
public String toString() {
return "" + num;
}
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Example example = (Example) o;
return num == example.num;
}
}
return num == example.num;