有没有办法将数组返回到另一个数组。
我有一个多维数组,我使用方法combineArrays(Comparable[][] x)
组合成另一个1D数组。
通常我只是在更高的范围内创建1D数组,但我痛苦地了解到Comparable数组在初始化时需要维度,而combineArrays()
负责确定mutliarray的大小。
请善待,我还没有完成。
程序本身的目的是接受一个对象数组,并找到每行中存在的公共对象列表。诀窍是findCommonElements()
必须在O(nlogn)或O(n)时间内这样做
/*
* combs through the original array to find the shortest row which must hold
* the least # of common objects
*/
public static int findSmallestRow(Comparable[][] queries) {
int array_length = 0;
int indexOfMaster = 0;
array_length = queries[0].length; // sets variable to initial arrays row
// length
for (int i = 0; i < queries.length; i++) { // iterates through each row
// comparing size of each
// row
if (queries[i].length <= array_length) { // ensures the 1st row is,
// at minimum, the
// master array
array_length = queries[i].length;
indexOfMaster = i;
}
}
return indexOfMaster;
}
public static void findCommonElements(Comparable[][] queries){
Comparable[] new_query = combineArray(queries);
for(int a = 0; a<new_query.length; a++){
System.out.println(new_query[a]);
}
//Arrays.sort(new_query);
int query_length = new_query.length;
int masterIndex = findSmallestRow(queries);
Comparable extracted[] = new Comparable[queries[masterIndex].length];
System.arraycopy(queries[masterIndex], 0, extracted, 0,
extracted.length);
Comparable[] intermediate_query = new Comparable[masterIndex];
int nonquery_length = extracted.length;
int counter = 0;
int counter2 = 0;
int query_index = 0;
int nonquery_index = 0;
int i =0;
int j = 0;
while(i < nonquery_length && j < query_length){
if(extracted[nonquery_index].compareTo(new_query[query_index])>0){
query_index++;
}
if(extracted[nonquery_index].compareTo(new_query[query_index])<0){
nonquery_index++;
}
if(extracted[nonquery_index].compareTo(new_query[query_index])==0){
counter++;
if(counter == queries.length){
intermediate_query[counter2] = extracted[nonquery_index];
counter2++;
}
}
}
Comparable common_list[] = new Comparable[counter2];
for(int k = 0; k<counter2; k++){
common_list[k] = intermediate_query[k];
System.out.println(common_list[i]);
}
}
//gets size of query array, even if not uniform
public static Comparable[] combineArray(Comparable[][]queries){
int length = queries.length;
ArrayList rows = new ArrayList();
for(int i = 0; i< length; i++){
for(int k = 0; k<queries[i].length; k++){
rows.add("");
}
}
int query_size = rows.size();
Comparable[] new_query = new Comparable[query_size];
int new_query_counter = 0;
for(int i = 0; i< length; i++){
for(int k = 0; k<queries[i].length; k++){
new_query[new_query_counter] = queries[i][k];
}
}
return new_query;
}
答案 0 :(得分:0)
这会计算数组所有行的集合交集。
public static Set<Comparable<?>> common( Comparable<?>[][] a ){
Set<Comparable<?>> inter = new HashSet<>( Arrays.asList( a[0] ) );
for( int i = 1; i < a.length; ++i ){
inter.retainAll( new HashSet<>( Arrays.asList( a[i] ) ) );
}
return inter;
}
最初的想法可能是保留重复的元素,即,如果有两个&#39; x&#39;在每一行中,结果还应包含两个&#39; x。给出的简短解决方案是使用集合,这些集合不存储相等元素的重复。 - 但是这里有一个库,你可以找到HashMultiSet,除了inter和result的类型之外,代码基本保持不变。
import org.apache.commons.collections4.multiset.HashMultiSet;