I am coding a program to output common elements of an array. I've got it working, but not to the standard it should be. Currently I have a function getCommonElements that prints each string index of where the string arrays have a commonality, but I have to return an empty string. I want to only return that string (array3) as a list of all the common elements. Thank you.
--The commented out part is the part that I have working, but want to replace. Currently this runs and gives an out of bounds error and I understand why, just want to change that.--
public class GetCommonElement {
public static String[] getCommonElements(String[] array1, String[] array2){
String[] array3 = {""};
for(int i =0; i < array1.length; i ){
for(int j = 0; j < array2.length; j ){
if (array1[i] == array2[j]){
/*System.out.print(array1[i]);
System.out.printf("\n");*/
String temp = array1[i];
for(int k = 0; k < array2.length; k ){
array3[k] = temp;
}
}
}
}
return array3;
}
TLDR: How do I compare two arrays and output the common elements into the new array.
CodePudding user response:
You can see here how to append an array. so you could start with an empty array String[] array3 = {};
and instead of array3[k] = temp;
you would do something like
array3 = Arrays.copyOf(array3, array3.length 1);
array3[array3.length - 1] =temp;
Alternatively you could count the matches then initialize an array of appropriate size. One problem I see with your code is that it will do strange things if multiples exist in each array. For example if the string "blah" existed twice in each array it would match 4 times. So for this reason I would probably do something like this which checks for redundancy:
public class matching{
public static String[] getCommonElements(String[] array1, String[] array2){
boolean[] matches = new boolean[array1.length];
boolean hasAMatch = false;
boolean isRedundant = false;
int nMatches = 0;
for(int i =0; i < array1.length; i ){
isRedundant = false;
for(int i2=0;i2<i;i2 ){
if(array1[i]==array1[i2]){
isRedundant = true;
break;
}
}
if(!isRedundant){
hasAMatch = false;
for(int j=0; j < array2.length; j ){
if (array1[i] == array2[j]){
hasAMatch = true;
nMatches ;
break;
}
}
if(hasAMatch){
matches[i] = true;
}
}
}
String[] array3 = new String[nMatches];
nMatches = 0;
for(int i =0; i < array1.length; i ){
if(matches[i]){
array3[nMatches] = array1[i];
nMatches ;
}
}
return(array3);
}
public static void main(String []args){
String[] a = {"blah","blah","didy","blah blah"};
String[] b = {"blah","ditty","blagh blah"};
String[] c = getCommonElements(a,b);
for(int i =0; i < c.length; i ){
System.out.println(c[i]);
}
}
}
CodePudding user response:
First of all, you should't be using the ==
operator to compare objects like String
. String
objects are cached for short ones, but in general there may be String
objects that have the same content but doesn't have the same memory address, so ==
will give you a false
. You should be using the String.equals(Object o)
method, or the null safe java.util.Objects.equals(Object o1, Object o2)
.
In addition, you don't know how many items match in the two arrays, so you don't know the length of your array3
result before the execution of the method. I recomend you to use a Set
, or if you want, a List
object for the result.
The code of the method might be something like the following:
public static String[] getCommonElements(String[] array1, String[] array2) {
List<String> coincidences = new ArrayList<>(
Math.min(array1.length, array2.length)
);
for (int i = 0; i < array1.length; i ) {
for (int j = 0; j < array2.length; j ) {
if (Objects.equals(array1[i], array2[j])
&& !coincidences.contains(array1[i])) {
coincidences.add(array1[i]);
}
}
}
return coincidences.stream().toArray(String[]::new);
}