編集:メソッドシグネチャ
public Comparable[][] findCommonElements(Comparable[][] collections)
間違っている。そのはず
public Comparable[] findCommonElements(Comparable[][] collections)
しかし、IDEで変更すると、すべてが台無しになります。セットを完全に理解しておらず、2D配列がひどく混乱しているため、知識を超えたような気がします。
2つのComparable配列を取り、線形時間効率でそれらを反復し、共通の要素を表示するアルゴリズムを作成する必要があります。HashSetを使用すると時間効率が最も速くなることを読みましたが、行き詰まりになりました。理由は次のとおりです。
指示と、メソッドのシグネチャである1行のコードが提供されました。
public Comparable[][] findCommonElements(Comparable[][] collections)
つまり、2D配列「コレクション」を返す必要があります。HashSetsの使用について教授にメールを送信したところ、次の問題がある場合を除いて、許可が与えられました。
「findCommonElementsメソッド内でHashSetを使用できますが、実行された比較の数をカウントできる必要があります。ハッシュは通常非常に効率的ですが、衝突が発生した場合にいくつかの比較が行われます。これを行うには、次のことを行う必要があります。使用するHashSetのソースコードにアクセスできます。また、比較の数を返すには、CommonElementsクラスに「getComparisons()」メソッドが必要です。」
プログラミングの2学期では、ハッシュセット、マップ、テーブルなどを学習しませんでした。これを自分で学習しようとしていますが、衝突について完全には理解していません。
私のコードは2つの配列を受け取り、共通の要素を返しますが、基本的にコンパイルするように記述したため、returnステートメントは厄介です(2d Comparable配列がパラメーターです)。
私はこれで正しい道を進んでいますか?コードは次のとおりです。
public class CommonElements {
static Comparable[] collection1 = {"A", "B", "C", "D", "E"}; //first array
static Comparable[] collection2 = {"A", "B", "C", "D", "E", "F", "G"}; //second array
static Comparable[][] collections = {collection1, collection2}; //array to store common elements.
static Set<Comparable> commonStuff = new HashSet<>(); //instance of Set containing common elements
public static void main(String[] args) {
CommonElements commonElements = new CommonElements(); //create instance of class CommonElements
commonElements.findCommonElements(collections); //call the find method
}
public Comparable[][] findCommonElements(Comparable[][] collections) {
Set<Comparable> addSet = new HashSet<>(); //instance of Set to add elements to
for (Comparable x : collection1) { //adding elements from first array to my addSet
addSet.add(x);
}
for (Comparable x : collection2) {
if (addSet.contains(x)) {
commonStuff.add(x); //checking for common elements, add to commonStuff Set
}
}
System.out.println(toString(commonStuff)); //print the toString method
return collections; //return statement, otherwise Java will whine at me
}
public String toString(Set<Comparable> commonStuff) { //this method gets rid of the brackets
String elements = commonStuff.toString(); //make a String and assign it to the Set
elements = elements.replaceAll("\\[", "").replaceAll("\\]", ""); //replace both brackets with empty space
return "Common Elements: " + elements; //return the Set as a new String
}
}
編集ApacheCommons ArrayUtilsをインポートしたことを忘れました。非常に便利。
私はそれを考え出した。あなたのすべての協力に感謝します。クラスのインスタンスを3回呼び出すmainメソッドと3つのテストメソッドがありますが、それらは関係ありません。これが私に問題を引き起こしていたものです、そして今それはうまくいきます。:-)
public int getComparisons() {
return comparisons;
} //method to return number of comparisons
public static Comparable[] findCommonElements(Comparable[][] collections) {
/*
I LEARNED THAT WE HAD TO USE MORE THAN TWO ARRAYS, SO IT WAS BACK
TO THE DRAWING BOARD FOR ME. I FIGURED IT OUT, THOUGH.
*/
Comparable[] arr1 = collections[0]; //set initial values to 1 Dimensional arrays so the test methods can read their respective values
Comparable[] arr2 = collections[1];
Comparable[] arr3 = collections[2];
/*
THE FOLLOWING BLOCK OF CODE TAKES ALL THE PERMUTATIONS OF THE 3 ARRAYS (i.e. 1,2,3; 1,3,2; 2,1,3, etc),
DETERMINES WHICH ARRAY IS THE SHORTEST, AND ADDS THE LONGER TWO ARRAYS TO A QUERY ARRAY.
*/
if(arr1.length < arr2.length && arr1.length < arr3.length || arr2.length <= arr3.length) { //shortest array will become hash array. the other two will become a combined query array.
hashArray = arr1; //these will be utilized below to put into Sets
queryArray = ArrayUtils.addAll(arr2, arr3);
}
else if(arr2.length < arr1.length && arr2.length < arr3.length || arr1.length <= arr3.length) {
hashArray = arr2;
queryArray = ArrayUtils.addAll(arr1, arr3);
}
else if(arr3.length < arr1.length && arr3.length < arr2.length || arr1.length <= arr2.length) {
hashArray = arr3;
queryArray = ArrayUtils.addAll(arr1, arr2);
}
HashSet<Comparable> intersectionSet = new HashSet<>(); //initialize Sets
HashSet<Comparable> arrayToHash = new HashSet<>();
for(Comparable element : hashArray) { //add shorter array to hashedArray Set
arrayToHash.add(element);
}
//NOTE FROM THE JAVADOC ON THE IMPLEMENTATION OF .contains() USING HASHSET COMPARISONS
/**
* <p>This class offers constant time performance for the basic operations
* (<tt>add</tt>, <tt>remove</tt>, <tt>contains</tt> and <tt>size</tt>),
* assuming the hash function disperses the elements properly among the
* buckets.
*/
for(Comparable element : queryArray) {
if(element != null) {
comparisons++; // increment comparisons with each search
}
if(arrayToHash.contains(element)) { //search for matches and add to intersectionSet (.contains uses the equals method to determine if an object is within array)
intersectionSet.add(element);
}
}
return intersectionSet.toArray(new Comparable[0]); //return Set as Array defined in method signature
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加