Thanks Anju for your reply,
Your code is very good - I have learnt
alot from it. There is one small problem with it, but that's due to me not explaining what I was trying to do properly. I think I should never post an abstract example on these forums again! I hope I'm forgiven (I haven't posted on a programming forum for a few years now). The only problem with the code is that the final '2' in array1, is being matched with the 1st '2' in the array2 (I wanted it matched with the 2nd '2' in array2).
Some of you may have wondered why I don't just clone array1 & then put it in the combined array - the reason is that the project I'm working on, not only contains ints, but other bits of information (which can't be matched) specific to what I called 'array2'.
Anyways, here's the code modified with new names & using vectors. I haven't used vectors much, and this took a few hours to figure out (initialising them is so much different than arrays). On the off-chance someone might have to do something similar, please find the code below (name the java file 'MatchingIntsWithVectors'). Thanks again to Anju, Stephen & Henry for comments and help
import java.util.Vector;
public class MachingIntsWithVectors{ // abstract matching ints demo
public static void main(String args[]) {
// trying to get: 3,3,2,2,1,1,2,2
Vector <Integer> v1 = new Vector<Integer>(); // initialise v1 vector (without generix x)
Integer a1 [] = {3,2,1,2}; // size is automatically set when adding elements
for (int i=0; i<a1.length; i++){ // add elements to vector using array
v1.add(a1[i]);
//System.out.print(v1.get(i) + " "); // debug
}
Vector <Integer> v2 = new Vector<Integer>(); // v2 vector
Integer a2 [] = {1,2,3,2};
for (int i=0; i<a2.length; i++){ // add elements to vector using array
v2.add(a2[i]);
//System.out.print(v2.get(i) + " "); // debug
}
Integer cvdef = -1; // needed to init cv vector's size in below for()
Vector <Integer> cv = new Vector<Integer>(); // Combined Vector
for (int i=0; i<v1.size() + v2.size(); i++) {
cv.add(cvdef);
}
int cvc=0; // Combined Vector Counter
for (int i=0; i<v1.size(); i++) //loop thru v1
{
cv.set( cvc, v1.get(i) ); // initialise cv to v1, starting at cv[0], then all remaining even indexes
//System.out.print(cv.get(cvc)+ "\t" ); // print out cv's even no indexes
for (int j=0; j<v2.size(); j++) // loop through v2
{
if (cv.get(cvc) == v2.get(j) ) //checks for exact match and if found then enters the loop
{
cvc+= 1; //increments the cv index before assigning the value to cv
cv.set(cvc, v2.get(j) ); //assigns second array value to cv
v2.remove(j); // remove this value so it's not found agn (only wana find & match ints once)
break; // breaks the for loop once the match is found and moves on to the first for loop
}//~if
}//~for
//System.out.print(cv.get(cvc)+ "\t" ); // print out vc's odd no indexes
cvc+=1; //increment cv index to assign the next value
}//~for
// */
// Print entire cv Vector
for (int i=0; i<cv.size(); i++){
System.out.print(cv.get(i) + " "); // debug
} System.out.print("\n" + cv.size() + "\n" + v2.size()); // size of cv & v2 printed (v2 should be 0, once all matches found)
System.out.println("\n\n"); // page-break
}//~main()
}//~class
[ June 07, 2006: Message edited by: Mohammed Malik ]