Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 21st, 2013, 07:21 PM
Ibcoding Ibcoding is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 5 Ibcoding User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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;
}


}

Reply With Quote
  #2  
Old February 21st, 2013, 07:22 PM
Ibcoding Ibcoding is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 5 Ibcoding User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 28 m 39 sec
Reputation Power: 0
Not the right number of comparisons

Reply With Quote
  #3  
Old February 21st, 2013, 07:23 PM
Ibcoding Ibcoding is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 5 Ibcoding User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 28 m 39 sec
Reputation Power: 0
Sorry for the formatting, posting from my phone.

Reply With Quote
  #4  
Old February 22nd, 2013, 05:46 AM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,961 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 3 h 23 m 31 sec
Reputation Power: 345
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,

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Number of comparisons

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap