ArrayList (Unions, Intersection, Difference, Etc)

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 4883 Views - Last Post: 18 November 2012 - 04:54 PM Rate Topic: -----

#16 lsutct  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 18-November 12

Re: ArrayList (Unions, Intersection, Difference, Etc)

Posted 18 November 2012 - 03:01 PM

hey i think we're working on the same project. I was wondering how you wrote the toString method, if you did it yet. Im confused about what we're supposed to format it with. thanks if you can help me
Was This Post Helpful? 0
  • +
  • -

#17 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: ArrayList (Unions, Intersection, Difference, Etc)

Posted 18 November 2012 - 03:46 PM

I wasn't comfortable with my suggested approach to an iterative solution using arrays - they can be tricky, so I created a small runnable example that builds on the ArrayList solution I posted earlier to include a solution with arrays:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class CrossProduct
{
    public static void main(String [] args)
    {
        String[] rank = { "Ace", "Two", "Three", "Four", "Five" };
        String[] suit = { "Spades", "Diamonds", "Clubs", "Hearts" };
        List<String[]> deckList = new ArrayList<String[]>();
        String[][] deckArray = new String[rank.length * suit.length][1];
        
        // places the cross product of the two arrays in an arraylist
        for ( int i = 0 ; i < rank.length ; i++ )
        {
            for ( int j = 0 ; j < suit.length ; j++ )
            {
                String[] newElement = { rank[i], suit[j] };
                deckList.add( newElement );
            }
        }

        System.out.println( "Printing the ArrayList of the X-Product:" );
        System.out.println( Arrays.deepToString( ( deckList.toArray() ) ) );
        
        // places the cross product of the two arrays in an array of arrays
        for ( int i = 0 ; i < rank.length ; i++ )
        {
            for ( int j = 0 ; j < suit.length ; j++ )
            {
                String[] newElement = { rank[i], suit[j] };
                deckArray[i * ( suit.length ) + j] =  newElement;
            }
        }

        System.out.println( "\nPrinting the Array containing the X-Product:" );
        System.out.println( Arrays.deepToString( ( deckArray ) ) );
        
    } // end method main()
    
} // end class CrossProduct

Was This Post Helpful? 0
  • +
  • -

#18 NewStudent709  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 14-November 12

Re: ArrayList (Unions, Intersection, Difference, Etc)

Posted 18 November 2012 - 04:09 PM

View Postlsutct, on 18 November 2012 - 03:01 PM, said:

hey i think we're working on the same project. I was wondering how you wrote the toString method, if you did it yet. Im confused about what we're supposed to format it with. thanks if you can help me


No, I'm still trying to figure out this Cartesian product method.
Was This Post Helpful? 0
  • +
  • -

#19 NewStudent709  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 14-November 12

Re: ArrayList (Unions, Intersection, Difference, Etc)

Posted 18 November 2012 - 04:54 PM

View PostGregBrannon, on 18 November 2012 - 03:46 PM, said:

I wasn't comfortable with my suggested approach to an iterative solution using arrays - they can be tricky, so I created a small runnable example that builds on the ArrayList solution I posted earlier to include a solution with arrays:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class CrossProduct
{
    public static void main(String [] args)
    {
        String[] rank = { "Ace", "Two", "Three", "Four", "Five" };
        String[] suit = { "Spades", "Diamonds", "Clubs", "Hearts" };
        List<String[]> deckList = new ArrayList<String[]>();
        String[][] deckArray = new String[rank.length * suit.length][1];
        
        // places the cross product of the two arrays in an arraylist
        for ( int i = 0 ; i < rank.length ; i++ )
        {
            for ( int j = 0 ; j < suit.length ; j++ )
            {
                String[] newElement = { rank[i], suit[j] };
                deckList.add( newElement );
            }
        }

        System.out.println( "Printing the ArrayList of the X-Product:" );
        System.out.println( Arrays.deepToString( ( deckList.toArray() ) ) );
        
        // places the cross product of the two arrays in an array of arrays
        for ( int i = 0 ; i < rank.length ; i++ )
        {
            for ( int j = 0 ; j < suit.length ; j++ )
            {
                String[] newElement = { rank[i], suit[j] };
                deckArray[i * ( suit.length ) + j] =  newElement;
            }
        }

        System.out.println( "\nPrinting the Array containing the X-Product:" );
        System.out.println( Arrays.deepToString( ( deckArray ) ) );
        
    } // end method main()
    
} // end class CrossProduct


I'm sorry, I'm still lost.

I have to implement the method under the class.

public Set xProduct(Set s)
   {
       int i;
       int j;
       ArrayList<String> result = new ArrayList<String>();
            for (i = 0 ; i < this.cardinality() ; i++ )
            {
                ArrayList<String> crossp = new ArrayList<String>();
                for (j = 0 ; j < s.cardinality() ; j++ )
                {
                    result.addAll(this.elements);
                    result.addAll(s.elements);
                }
               result.addAll(crossp);
            }
            return new Set(result);
   }



And it's telling me this.

Exception in thread "main" java.lang.IllegalArgumentException: Set(ArrayList<String>)duplicity not allowed in sets
(S1 ⊖ S2) ∪ (S1 ∩ S2) ∪ (S2 ⊖ S1)
{[2, 4, 6, 8]} ⊖ {[2, 3, 5, 7]} ∪ {[2, 4, 6, 8]} ∩ {[2, 3, 5, 7]} ∪ {[2, 3, 5, 7]} ⊖ {[2, 4, 6, 8]} = {[4, 6, 8, 2, 3, 5, 7]}
	at setapplication.Set.<init>(Set.java:34)
	at setapplication.Set.xProduct(Set.java:188)
	at setapplication.Set.main(Set.java:278)
Java Result: 1


Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2