8 Replies - 542 Views - Last Post: 01 March 2013 - 10:48 AM Rate Topic: -----

#1 iburres  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 148
  • Joined: 05-September 12

Comparable collections

Posted 28 February 2013 - 09:14 PM

I am at the end of my rope trying to figure this one out. Unfortunately, the only resource I have is a book, and I am simply not "getting it." The idea here is very simple. I want to pass two arrays containing objects of type comparable into the method findCommonElements. The, I want to find the objects that are similar. Finally, I want to store those values into an empty array, so that I can print this new array, which should contain all the common elements. I know I can't compare a single array index with a comparable object. I've stepped away from this several times. I even drew up a UML diagram. So if someone could show me what in the world it is I am doing wrong, I might actually continue trying to learn this stuff. :wacko:


public class Test   {

public static void main(String[] args)  {
		Integer[] coll_1 = {1, 2, 4, 6, 10, 12, 13, 14, 15, 20};
		Integer[] coll_2 = {4, 6, 8, 9, 10, 11, 15, 19, 21, 23};
        Object[] collections = new Object[2];
        collections[0] = coll_1;
        collections[1] = coll_2;
        CommonElements.findCommonElements(collections);
        
        
	 }
       
	
}





public class CommonElements implements Comparable<Integer>{
    
	private static int comparisons = 0;
	private static Integer[] common = {};	
	
public int getComparisons() {
	return comparisons;
}
	
	 public static Comparable[] findCommonElements(Object[] collections) 
     {  
		 Integer[] query = {1, 2, 3, 4, 9, 20, 21, 30, 31, 35 };
		 int i;
		 
         for( i = 0; i < collections.length; i++ ){
        	 comparisons++;
        	 if(query.equals( collections[i] )) {
        		common = (Integer[]) collections[i];
        		 
        	 }
         
         }
         System.out.println("The following elements were common among all arrays: " + "\n");
         System.out.println( common);
         System.out.println("\n" + "Additionally, there were a total of " +  comparisons  + " comparisons made between the arrays." + "\n");
         	return common;
         	
         }

	
	public int compareTo(Integer collections) {
		
		return 0;
		
	}
	
}


	    	          







Output:

The following elements were common among all arrays:

[Ljava.lang.Integer;@c21495

Additionally, there were a total of 2 comparisons made between the arrays.

Is This A Good Question/Topic? 0
  • +

Replies To: Comparable collections

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Comparable collections

Posted 28 February 2013 - 09:46 PM

And the question is ?
I can't beleive iburres how again you are complicating your life
You will never succeed in the programming business if you always complicate your life as you do
Was This Post Helpful? 0
  • +
  • -

#3 iburres  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 148
  • Joined: 05-September 12

Re: Comparable collections

Posted 28 February 2013 - 09:54 PM

Lol. How can I get rid of [Ljava.lang.Integer;@c21495 and instead get
{1, 2, 4, 9, 21 }? I can't use hash sets, god knows why.

Seriously though, that made me laugh. Much needed at this point.

This post has been edited by iburres: 28 February 2013 - 09:55 PM

Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2687
  • View blog
  • Posts: 11,349
  • Joined: 20-September 08

Re: Comparable collections

Posted 01 March 2013 - 05:31 AM

System.out.println(java.util.Arrays.toString(common));


Quote

I can't use hash sets, god knows why.
You don't need to actually. What's the exact brief?

This post has been edited by g00se: 01 March 2013 - 05:47 AM
Reason for edit:: Clarification

Was This Post Helpful? 1
  • +
  • -

#5 iburres  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 148
  • Joined: 05-September 12

Re: Comparable collections

Posted 01 March 2013 - 10:08 AM

When I input that line I get an array with 10 null values. So obviously my method is still not working.

The brief is this:

1. It should be able to accept as input 0 to k collections, stored as simple arrays.
2. The elements of the collections should all be of type Comparable, and they should all be
derived from the same base class (not counting the Object class). Implementation of the
Comparable interface is necessary since the elements must be compared to each other in
order to determine commonality. They must all be derived from the same base class in
order for comparisons between two items to be meaningful.
3. Duplicate elements should be allowed; e.g., if there are M instances of the value, “XYZ”, in
all the input collections, there should be M instances of the value, “XYZ”, in the collection of
common elements.
4. The collections should be allowed to be of varying lengths; i.e., some collections may have
more items than others.
5. One of the collections must be designated as the “query” collection, which is the collection
containing the elements to which the elements in the other collections are compared.
6. The total number of element comparisons performed should be less than the value for the
quadratic solution described above. That is, the total number of comparisons in the worst
case should be less than kN2
. Do not be concerned about average performance. Also, the
total number of comparisons is defined, for this assignment, to be only those comparisons

The framework for your algorithm should satisfy the following criteria, for ease in testing:
1. Create a class called CommonElements, to contain your algorithm and associated methods and attributes.
2. In your CommonElements class, encapsulate your algorithm within a method called findCommonElements, that has the following signature:

public Comparable[] findCommonElements(Object[] collections).

The argument to this method, collections, will be the set of k collections discussed earlier. Each collection will be represented as an array of objects of type Comparable.
3. The value returned by your findCommonElements method should be a collection of Comparable elements that contains only the elements common to all the input collections.
4. Since you are being asked to evaluate your algorithm based on the number of comparisons performed, you will need to have your findCommonElements method maintain a running total of comparisons performed for each set of collections tested. You should create an attribute
called comparisons in your CommonElements class to store the number of comparisons, and provide a getter method called getComparisons() to return this value. In order to keep a running total of comparisons, you will need to instrument your code by incrementing the comparisons attribute each time a comparison between two elements is made. Since element comparisons are typically performed in if statements, you may need to increment
comparisons immediately before each comparison is actually performed. Although that may sound counter-intuitive, if you try to increment comparisons inside the if statement, after the element comparison has been made, you will miss all the comparisons that cause the condition inside the if statement to evaluate to false.

This post has been edited by iburres: 01 March 2013 - 10:09 AM

Was This Post Helpful? 0
  • +
  • -

#6 iburres  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 148
  • Joined: 05-September 12

Re: Comparable collections

Posted 01 March 2013 - 10:15 AM

I have no idea why, but right after posting this I made everything Comparable, and now I have an array filled with common elements. They are not all of the correct elements, but it's a start.

The following elements were common among all arrays:

[4, 6, 8, 9, 10, 11, 15, 19, 21, 23]

Additionally, there were a total of 2 comparisons made between the arrays.


Which is just collection[1]

This post has been edited by iburres: 01 March 2013 - 10:16 AM

Was This Post Helpful? 0
  • +
  • -

#7 iburres  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 148
  • Joined: 05-September 12

Re: Comparable collections

Posted 01 March 2013 - 10:23 AM

 public static Comparable[] findCommonElements(Object[] collections) 
     {  
		 Comparable[] query = {1, 2, 3, 4, 9, 20, 21, 30, 31, 35 };
		 int i;
         for( i = 0; i < collections.length; i++ ){
        	 
        	 comparisons++;
        	 if(collections[i].equals((Comparable[]) collections[i] )) {
        		common =  (Comparable[]) collections[i];
        		 
        	 }
         
         }
         System.out.println("The following elements were common among all arrays: " + "\n");
         System.out.println(java.util.Arrays.toString( common));
         System.out.println("\n" + "Additionally, there were a total of " +  comparisons  + " comparisons made between the arrays." + "\n");
         	return common;
         	
         }


This post has been edited by iburres: 01 March 2013 - 10:24 AM

Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Comparable collections

Posted 01 March 2013 - 10:47 AM

What about
	public static void main(String[] args) {
		Integer[] x = {3, 8, 4, 7, 12, 18, 19, 23, 78};
		Integer[] y = {23, 88, 45, 8, 17, 16};
		Comparable[] ar = compare(x, y);
		for(int i = 0; i < ar.length; ++i)
			System.out.println(ar[i]);
	}
	
	private static Comparable[] compare(Comparable[] first, Comparable[] second) {
		ArrayList<Comparable> al = new ArrayList<Comparable>();
		for(int i = 0; i < first.length; ++i) {
			for(int j = 0; j < second.length; j++) {
				if(first[i].equals(second[j])) {
					al.add(first[i]);
					break;
				}
			}
		}
		Comparable[] out = al.toArray(new Comparable[0]);
		return out;
	}


Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10467
  • View blog
  • Posts: 38,799
  • Joined: 27-December 08

Re: Comparable collections

Posted 01 March 2013 - 10:48 AM

A couple points. First, arrays do not override the Object toString() method. Hence, you get the class name and memory location as the result of toString(), which is why you can't just dump an array into the println() method.

Second, Comparable is generic. Using a Comparable[] will produce a deprecation warning. Use generics here.
public <T extends Comparable<? super T>> foo(T[] array){}



Here, array contains elements that are mutually Comparable. This ensures you won't be comparing Apples and Oranges, so to speak.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1