?
Solved

How to subtract two Vector (of string arrays) ? : JAVA

Posted on 2005-03-19
31
Medium Priority
?
1,310 Views
Last Modified: 2010-03-31
Hi,
I will have two JDBC query and I want to store the results in two Vectors.
The two vectors will have some common elements.
I would like to subtract once vector from the other.
Here is the code snippet:
>>I will have a Vector that holds all the classes taken by a student,
and I will have another Vector that holds all the classes that are required.
I want to find out the required classes that are not taken by the student.
Thus I will need to subtract one vector from the other.

Assuming Vector A holds all the required classes taken by the student,
and Vector B holds all the required classes for the student.
How do I subtract Vector A from Vector B.
BOTH MY VECTORS HOLD  ARRAYS OF STRINGS.

Please advise.



Thanks
_Esam
0
Comment
Question by:_Esam
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 18
  • 9
  • 4
31 Comments
 
LVL 2

Expert Comment

by:ucool
ID: 13583407
you can use  this Vector method :

boolean       removeAll(Collection c)
          Removes from this Vector all of its elements that are contained in the specified Collection.
0
 

Author Comment

by:_Esam
ID: 13583425
So, I can simply say>>
If Vector A is the superset and Vector B is the subset
>>  A.removeAll(B)   ???

This does it???

_Esam
0
 
LVL 2

Assisted Solution

by:ucool
ucool earned 160 total points
ID: 13583437
See this example:
http://java.sun.com/docs/books/tutorial/collections/interfaces/set.html

<cut>
import java.util.*;

public class FindDups2 {
    public static void main(String args[]) {
        Set uniques = new HashSet();
        Set dups = new HashSet();

        for (int i=0; i<args.length; i++)
            if (!uniques.add(args[i]))
                dups.add(args[i]);

        uniques.removeAll(dups);  // Destructive set-difference

        System.out.println("Unique words:    " + uniques);
        System.out.println("Duplicate words: " + dups);
    }
}
</cut>
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:_Esam
ID: 13583489
I understand that it's easy to get a difference of two sets.
Essentially sets are just unique values of a single data type i.e. a list of a type with unique values???

I have to do the difference with Vectors...?
Of course Vectors are collections.
And same boolean removeAll(Collection c) applies to Vectors? as it does to sets ??

Thing is my Vectors are not just simple set or list>>
They are arrays of strings >>>
That's why I was confused if same difference rule applies to Vectors or Not??

_Esam

0
 
LVL 2

Expert Comment

by:ucool
ID: 13583542
I convert it as using Vector, give a try and let me know what your string looks like:

import java.util.*;

public class SubtractVect {
    public static void main(String args[]) {
        Vector requires = new Vector(10);
        Vector regists = new Vector(10);


      requires.add("math");
      requires.add("Chemistry");
      requires.add("English");

        regists.add("math");
      regists.add("English");

        requires.removeAll(regists);  // Destructive Vector-difference

        System.out.println("Required classes which are not registered:    " + requires);
        System.out.println("Regists classes: " + regists);
    }
}
0
 

Author Comment

by:_Esam
ID: 13583617
I am still not clear:

What if my requires vectors is like:     "Math" " 410"           "English" "210"              and my regists is like:   "Math" " 410"

Where my Vectors hold arrays of strings:::
Can I still subtract them??

_Esam
0
 
LVL 2

Expert Comment

by:ucool
ID: 13583684
Give me a example how you construct your Vector with string array.
for example: regists is like:   "Math" " 410"
How your RegistsVector look like?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13583844
>>I want to find out the required classes that are not taken by the student.

Why not do that directly in the sql query?
0
 

Author Comment

by:_Esam
ID: 13584963
Yes, indeed.
I thought about doing that in the query ---
That should simplify it - rather than having two queries and two Vectors and
a substraction where one query could do it.
I was just trying to see how I can do it with Vectors...
Subtract two Vectors of arrays of strings>?? possible??


!!!!

_Esama
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 240 total points
ID: 13585510
>>
That should simplify it - rather than having two queries and two Vectors and
a substraction where one query could do it.
>>

Indeed it would

>>Subtract two Vectors of arrays of strings>?? possible??

It is possible yes, but to be avoided probably ;-)


0
 

Author Comment

by:_Esam
ID: 13592012
I happen to have two vectors that I needed ........for other parts of the program
If I can get the result with subtracting them ..
WHy do another query..??

Unless I do some changes the program itself..
>>It is possible yes, but to be avoided probably ;-)

How... simple illustraiton or reference of said type would do..

_Esam

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13594533
>>How... simple illustraiton or reference of said type would do..

Do you mean you want to see the type of query you could do?
0
 

Author Comment

by:_Esam
ID: 13598201
How about both:
If I have two queries like:

Get the required courses:
String sqlQuery = "SELECT COURSES.COURSE_ID, COURSES.COURSE_NAME FROM COURSES, REQUIREMENTS, PROGRAMS, STUDENT WHERE COURSES.COURSE_ID = REQUIREMENTS.COURSE_ID_DUP AND REQUIREMENTS.PROGRAM_NAME=PROGRAMS.PROGRAM_NAME AND PROGRAMS.PROGRAM_NAME = STUDENT.PROGRAM_NAME AND STUDENT.UST_ID =" + ID + "AND REQUIREMENTS.REQUIRED = 'R'";


Gets the required courses taken by the student:

 String sqlQuery = "SELECT COURSES.COURSE_ID, COURSES.COURSE_NAME FROM STUDENT, REQUIREMENTS, COURSES_TAKEN, COURSES WHERE STUDENT.PROGRAM_NAME = REQUIREMENTS.PROGRAM_NAME AND REQUIREMENTS.COURSE_ID_DUP = COURSES_TAKEN.COURSE_ID AND STUDENT.UST_ID = COURSES_TAKEN.UST_ID AND COURSES_TAKEN.COURSE_ID = COURSES.COURSE_ID AND REQUIREMENTS.REQUIRED = 'R' AND STUDENT.UST_ID =" + ID;


How can I combine these two in one sql?

Or better: if I have two Vectors holding the results of each sql return, how do I get a difference??

_Esam
0
 

Author Comment

by:_Esam
ID: 13599767
I use the Vector allOptVector and String[] aReqRow as illustrated below:
I put the results in the allOptVector:
If I have two vectors that hold the data from the queris on my last post...
How to I subtract them or get the difference??

while(resultSet.next())
           {
            aReqRow = new String[numColumns];
            for (int i = 0; i < numColumns; i++)
            aReqRow[i] = resultSet.getString(i+1);
            allOptVector.addElement(aReqRow);
           }

Please let me know....
As suggested by the other expert VectorA.removeAll(VectorB) doesn't work???

Thanks.

_Esam
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13599787
We'd better continue the discussion in your other Q a bit first as i'm unclear about the schema
0
 

Author Comment

by:_Esam
ID: 13599937
The database schema that I used was:
STUDENT

UST_ID (PK)
PROGRAM_NAME (references PROGRAM_NAME in PROGRAMS)

COURSES_TAKEN
UST_ID   (PK)  (references UST_ID in STUDENT)
COURSE_ID (PK) (reference COURSE_ID in COURSES)

COURSES

COURSE_ID (PK)
DESCRIPTION

REQUIREMENTS

COURSE_ID_DUP (PK) (references COURSE_ID in COURSES)
PROGRAM_NAME (PK) (references PROGRAM_NAME in PROGRAMS)
REQUIRED

PROGRAMS

PROGRAM_NAME (PK)
PROGRAM_DESCRIPTION

I was provided with some raw text data that I hurriedly converted to the database.
I don't like Access (not flexible?).....better started with Oracle...

Can a filed typed text be not a Primary Key? as in the case of PROGRAMS?

_Esam
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13599960
>>Can a filed typed text be not a Primary Key?

Not sure what you mean by that. Can you explain the programs table too?
0
 

Author Comment

by:_Esam
ID: 13599984
That's all there is:

 Table PROGRAMS

  Column1 PROGRAM_NAME (PK, Text)
  Column2 PROGRAM_DESCRIPTION (Text)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13600008
Yes, but what is it *about*? What are programs, how do they relate to courses?
0
 

Author Comment

by:_Esam
ID: 13600088
PROGRAM_NAME from PROGRAM is related to PROGRAM_NAME in STUDENT.

STUDENT is related to COURSES_TAKEN by UST_ID

COURSES_TAKEN (in addition to COURSES) is related to REQUIREMENTS by COURSE_ID.

REQUIREMENTS is related to PROGRAM by PROGRAM_NAME.

So,
PROGRAMS relate to COURSES via STUDENT >COURSES_TAKEN>REQUIREMENTS?

_Esam
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13600112
No i don't mean that ;-) You still haven't told me what the programs table *is*
0
 

Author Comment

by:_Esam
ID: 13600165
You mean the actaul data like:

PROGRAM_NAME: BSCS
PROGRAM_DESCRIPTION: Bachelor of Science: Computer Science
PROGRAM_NAME: MSCS
PROGRAM_DESCRIPTION: Master's of Science: Computer Science

_Esam
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13600209
Well not really, but that helps slightly ;-) Perhaps this question will throw a bit more light on things: why did you include that table in your join before?
0
 

Author Comment

by:_Esam
ID: 13600279
Good question.
I included it to get the PROGRAM_NAME.
I really don't need to do that for what I need to do.
I can get the PROGRAM_NAME from STUDENT table for a particular student.

Now what?

_Esam
0
 

Author Comment

by:_Esam
ID: 13600346
Indeed, I used the PROGRAM table for no good reason in the First query .
The same kind of result was acheived in the second query without the PROGRAM table.
It used the REQUIREMENTS and STUDENT table to relate the PROGRAM_NAME.

_Esam
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13600438
So that query could be:

select c.course_id, c.course_name from student s, requirements r, courses_taken ct, courses c where s.ust_id = ct.ust_id and c.course_id = ct.course_id and r.course_id_dup = ct.course_id and r.required = 'R' and s.ust_id = " + id;

?
0
 

Author Comment

by:_Esam
ID: 13600546
Yes that' right...
Now I get all the problem when I try to get the 'R' (REQUIRED) courses not taken by a student.

_Esam
0
 

Author Comment

by:_Esam
ID: 13600572
Well, since you are making things clearer...
Here is what I have to confess....

There still is a little design flaw....

I am trying to figure out how to handle that...

In the REQUIREMENT table....
Although there is a COURSE_ID_DUP,
Say CSIS530, it can be replaced with CSIS531 (Complementary)
I can't still figure out how I can do that..
Any suggestion?? (TO make things little more complicated for better :)

_Esam
0
 

Author Comment

by:_Esam
ID: 13600755
I am too tired...
I have to take a nap...in this early morning..:)
Can't promise when will be back....will continue..

Thanks.

_Esam
0
 

Author Comment

by:_Esam
ID: 13627826
Ok CEHJ,
What to do with this question...it's somewhat forgotten??

Where should we start...?

How about subtracting two Vectors of arrays of strings???

_Esam
0
 

Author Comment

by:_Esam
ID: 13667375
I ended up doing this in the sql ...

Thanks.

_Esam
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month8 days, 12 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question