Can you compare arrays of different lengths?

Asked 2 years ago, Updated 2 years ago, 144 views

public static void main(String[] args) {

int[] array1 = new int[4];

int[] array2 = new int[7];

Arrays.equals(array1,array2);

}

I don't get an error even if I do this, but I'm not sure if I compared it properly. If you do this, I ask you if you only compare the length of array1 and not the rest.

array java equals

2022-09-22 18:55

1 Answers

You registered almost the same question below.The simplest way is to use stream.

import java.util.*;
import java.util.stream.*;

List<Integer> arr1 = Arrays.asList(1, 2, 3, 4, 5, 6);
List<Integer> tmp = Arrays.asList(2, 3, 6);
List<Integer> result = arr1.stream().filter(tmp::contains).collect(Collectors.toList())); // arr1 and tmp intersection, i.e., extract only the values that exist

// The result stores [2, 3, 6].


2022-09-22 18:55

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.