tags:

views:

161

answers:

6

Hello! I have some problem, i want to union, intersect, difference and reverse operator in java.

first i have 2 arraylist

a = [0,2,4,5,6,8,10]
b = [5,6,7,8,9,10]

a union b is return c = [0,2,3,4,5,6,7,8,9,10]

a intersect b is return c = [5,8,10]

a defference b is return c = [0,2,3,4]

reverse a = [10,8,6,5,4,2,0]

something like this.

how to implement that method in java?


Update: I have to start with this template:

package IntSet;
import java.util.ArrayList;
import java.util.Collection;


public class IntSet {

private ArrayList<Integer> intset;

public IntSet(){
    intset = new ArrayList<Integer>();
}

public void insert(int x){
    intset.add(x);
}

public void remove(int x){
    //implement here
    intset.indexOf(x);
}

public boolean member(int x){
    //implement here
    return true;
}

public IntSet intersect(IntSet a){
    //implement here
    return a;
}

public IntSet union(IntSet a){
    //implement here
    return a;
}

public IntSet difference(IntSet a){
    //implement here
    IntSet b = new IntSet();
    return b; 
}
A: 

Take a look at ListUtils in Apache common collections

RC
+1  A: 

Take a look at answers of Classical set operations for java.util.Collection

Ankit Jain
+5  A: 

First, the operations you describe (except reverse) are set operations, not list operations, so use HashSet or (if you need an ordering) TreeSet.

    Set<Integer> a = new TreeSet<Integer>(Arrays.asList(new Integer[]{0,2,4,5,6,8,10}));
    Set<Integer> b = new TreeSet<Integer>(Arrays.asList(new Integer[]{5,6,7,8,9,10}));

    //union
    Set<Integer> c = new TreeSet<Integer>(a);
    c.addAll(b);
    System.out.println(c);

    //intersection
    Set<Integer> d = new TreeSet<Integer>(a);
    d.retainAll(b);
    System.out.println(d);

    //difference
    Set<Integer> e = new TreeSet<Integer>(a);
    e.removeAll(b);
    System.out.println(e);

    //reverse
    List<Integer> list = new ArrayList<Integer>(a);
    java.util.Collections.reverse(list);
    System.out.println(list);
Landei
+1  A: 
//Union 
List<Integer> c = new ArrayList<Integer>(a.size() + b.size());
addNoDups(c,a);
addNoDups(c,b);

private void addNoDups(List<Integer> toAddTo,List<Integer> iterateOver) {
    for(Integer num:iterateOver){
        if(toAddTo.indexOf(num) == -1) {
            toAddTo.add(num);
        }
    }
}

//intersection
List<Integer> c = new ArrayList<Integer> (a.size() > b.size() ?a.size():b.size());
c.addAll(a);
c.retainAll(b);

//difference a-b
List<Integer> c = new ArrayList<Integer> (a.size());
c.addAll(a);
c.removeAll(b);
anand
A: 

Like always, using Guava libraries may lead you to clean and efficient solutions to this problem as well. Guava's Sets utiliy API has what you need.

nabeelalimemon
+1  A: 

a lot of the answers tell you to use libraries that will do the work for you. while this is the right solution for the real world, keep in mind hat you're doing homework, and your teacher probably wants you to understand how the functions are written, not just how to find libraries to do the work for you.

that said, you've got a good start with the code you've shown. let's take the problem one step at a time.

First, do you know where the Java documentation is located? http://download.oracle.com/javase/1.4.2/docs/api/ this is critical, as this is how you find out what functions do what. Here's the link to Java 1.4. I didn't notice what version you're using, but Java is backward comatable, so this should be sufficient.

In the docs, find the ArrayList entry.

Now that we've got the API docs, we need to break down your question. you've posted code, so I'll address it function by function.

insert() : do you have to have an ordered list, or does order not matter? Or are you guraranteed that values will be provided to you in order? Have you learned sorting algorithms yet?

remove() : this function doesn't work. take a look at the ArrayList API and see how to remove an item from the list. use that method.

member() : your member method doesn't work. you need to check every entry of the list and determine if the current member matches the function argument. Have you learned about for loops?

intersect() : ok, tell me in english what intersect is supposed to do. Don't use the teacher's description if you can help it - use your own words (note to others, this is an exercise for the OP to learn to program, so please don't go answering it for him)

difference() : again, tell me in english what it's supposed to do.

reverse() : again, give me the neglish description of what this is supposed to do.

Once you have english descriptions, describe an algorithm that can do the work. don't write it in Java yet. just write an algorithm, in english, describing how you would do the work manaully, with pen and paper.

at this point, try and convert the algorithm to Java code.

atk