The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Java Help
|
Number of comparisons
Discuss Number of comparisons in the Java Help forum on Dev Shed. Number of comparisons Java Help forum discussing all Java platforms - J2ME, J2SE and J2EE - as well as relevant standards, APIs and frameworks such as Swing, Servlets, JSPs, Applets, Struts, Spring, Hibernate, ANT, EJB, and other Java-related topics.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

February 21st, 2013, 07:21 PM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 5
Time spent in forums: 28 m 39 sec
Reputation Power: 0
|
|
|
Number of comparisons
Ok, can anyone point out the exact location(s) of my errors. I'm getting the appropriate common elements, but the right number of comparisons.
public class Test {
public static void main(String[] args) {
Integer[] collection1 = {1, 2, 3, 4, 5, 16};
Integer[] collection2 = {1, 2, 3, 4, 16, 21};
Integer[] collection3 = {1, 2, 3, 4, 5, 6, 7, 8};
Integer[] collection4 = {1, 2, 3, 4, 6, 21};
Object[] storeAllArray = {collection1, collection2, collection3};
System.out.println(CommonElements.findCommonElements(storeAllArray));
System.out.println("Comparison: " + CommonElements.getComparisons());
}
}
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class CommonElements {
public static int comparisons = 0;
public static Object[] collections;
public static Object[] common = new Comparable[20];
public int count = 0;
public static int getComparisons()
{
return comparisons;
}
public void setComparisons(int comparisons)
{
CommonElements.comparisons = comparisons;
}
public static Comparable<?> getNumber()
{
return getNumber();
}
public static int getIndexOfLowest(Object[] collections)
{
int indexOfLowest = 0;
int size = 0;
for(int i = 0; i < collections.length; i++)
{
Arrays.sort((Comparable[])collections[i]);
size = ((Comparable[])collections[i]).length - 1;
if(size < ((Comparable[])collections[indexOfLowest]).length)
{
indexOfLowest = i;
}
}
System.out.println("The common elements in the arrays are: " );
return indexOfLowest;
}
public static Comparable[] findCommonElements(Object[] collections)
{
int number = getIndexOfLowest(collections);
Set<Object> commonElements = new HashSet<Object>();
for(int i = 0; i < collections.length; i++)
{
Set<Object> hashSet = new HashSet<Object>(Arrays.asList(collections[number]));
comparisons++;
if(hashSet.contains(collections[i]))
{
commonElements.add(collections[i]);
}
}
Object[] collection = commonElements.toArray(new Object[0]);
System.out.println(Arrays.deepToString(collection));
return (Comparable[]) common;
}
}
|

February 21st, 2013, 07:22 PM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 5
Time spent in forums: 28 m 39 sec
Reputation Power: 0
|
|
|
Not the right number of comparisons
|

February 21st, 2013, 07:23 PM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 5
Time spent in forums: 28 m 39 sec
Reputation Power: 0
|
|
|
Sorry for the formatting, posting from my phone.
|

February 22nd, 2013, 05:46 AM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
|
Please edit the post and wrap the code in code tags.
Can you explain what the problem is? Post the program's output and add some comments saying what is wrong with it and show what it should be,
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|