[英]Merge two String Arrays using only loops
I'm having some issues with a simple task that I'm not being able to understand. 我遇到一个我无法理解的简单任务问题。 I've got a simple task which is to create an alghoritm that merges two arrays but without repetitions only using for loops...
我有一个简单的任务是创建一个合并两个数组但不重复的算法,仅使用for循环...
This is my code so far... 到目前为止,这是我的代码...
public static String[] mergeReps(String[] a1, String[] a2) {
// Create a new array with the full size (I'll remove the nulls later)
String[] retArray = new String[a1.length + a2.length];
// Copy of array a1 to the retArray
retArray = Arrays.copyOf(a1, a1.length);
// Test print...
System.out.println(Arrays.toString(retArray));
// loop to check if the indexes value are present in the a1 array
for (int i = 0; i < a1.length - 1; i++) {
for (int j = i + 1; j < a2.length; j++) {
// Condition to check if the value is duplicated
if (!(a1[j].equalsIgnoreCase(a2[i]))) {
retArray[i + a1.length] = a2[j];
}
}
}
return retArray;
}
My question is: How can I compare a2[0] to every single position in the a1 array, and only after doing that and knowing if it's duplicate or not, add it to the retArray? 我的问题是:我怎样才能将a2 [0]与a1数组中的每个位置进行比较,并且只有在这样做并知道其是否重复之后,才将其添加到retArray中?
Try this code Snippet. 尝试使用此代码段。 Basically it uses the uniqueness property of a set to compile a set of unique string values.
基本上,它使用集合的uniqueness属性来编译一组唯一的字符串值。 It then converts it to String[].
然后将其转换为String []。
Check out the javadocs for HashSets for more information on how they work. 查看HashSets的javadocs,以获取有关其工作方式的更多信息。
Set<String> result = new HashSet<String>();
for(String s : a1)
{
result.add(s);
}
for(String s : a2)
{
result.add(s)
}
return result.toArray(new String[result.size()]);
You can use this: 您可以使用此:
for(int nextItemOfSecondArray = a2.length -1; nextItemOfSecondArray >= 0; i--){
boolean checkIfAlreadyExists = false;
for(int i = 0; i < a2.length; i++){
if(a2[nextItemOfSecondArray].equalsIgnoreCase(a1[i])){
checkIfAlreadyExists = true;
break;
}
}
if(!checkIfAlreadyExists)
a1[a1.length] = a2[nextItemOfSecondArray];
}
Assuming a1 and a2 each have no duplicates, here is how to merge the arrays without duplicates: 假设a1和a2都没有重复项,这是如何合并没有重复项的数组:
public static String[] mergeReps(String[] a1, String[] a2) {
//validate the incoming arrays
if (a1 == null && a2 == null) {
System.err.println("error - input arrays are null");
return new String[]{};
} else if (a1 == null) {
return a2;
} else if (a2 == null) {
return a1;
}
int arrEffSize = 0;
boolean unique;
String s, sfinal;
String[] tempArray = new String[a1.length + a2.length];
//just copy a1 to the tempArray
System.arraycopy(a1, 0, tempArray, 0, a1.length);
arrEffSize = a1.length;
//add String objects from a2, if it isn't already there
for (int i=0; i < a2.length; i++) {
unique = true;
s = a2[i];
for (int j=0; j < arrEffSize; j++) {
sfinal = tempArray[j];
if (s.equalsIgnoreCase(sfinal)) {
unique = false;
break;
}
}
if (unique) {
tempArray[arrEffSize] = s;
arrEffSize++;
}
}
//create a new array with the appropriate size, then copy tempArray to it
String[] retArray = new String[arrEffSize];
System.arraycopy(tempArray, 0, retArray, 0, retArray.length);
return retArray;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.