Java and vector help....

Bulldog13

Golden Member
Jul 18, 2002
1,655
1
81
Here s my problem.

I have 2 vectors. myVector and temp.

If myVector is empty or equal to null I just want to copy the value of temp into it.
If myVector is not empty then I want to append temp to it.

Here is my code:

if (myVector == null){
myVector = DocParser1.getVector();
System.out.println("Vector equals null" + myVector);
}
else {
myVector.addAll(temp);
System.out.println("Vector does not equal null" + myVector);
}

The code works fine when myVector is equal to null.

But if I add this line of code..

myVector.add(0,"HELLO");

Before the block if statement then nothing happens. Even the println statement does not execute.



I have tried the isEmpty function.

Any ideas?

 

OulOat

Diamond Member
Aug 8, 2002
5,769
0
0
Next time: A) state what language. B) State which compiler. C) Read the documentation.

Assuming this is JAVA. Read the Java API

add(int index, Object element)
Inserts the specified element at the specified position in this Vector.

A string is not an object. Try myVector.add(0, (Object) "HELLO");
 

manly

Lifer
Jan 25, 2000
13,590
4,239
136
Originally posted by: OulOat
Next time: A) state what language. B) State which compiler. C) Read the documentation.

Assuming this is JAVA. Read the Java API

add(int index, Object element)
Inserts the specified element at the specified position in this Vector.

A string is not an object. Try myVector.add(0, (Object) "HELLO");
Besides RTFM you gave some lousy, inaccurate advice.
 

Kilrsat

Golden Member
Jul 16, 2001
1,072
0
0
A string is not an object. Try myVector.add(0, (Object) "HELLO");

Yes, a string is an object. So you can add it directly without casting it to the object class. I just slapped together some test code of my own.

Here's what I have:

static Vector myVector;
static Vector temp;

public static void main(String[] args) {
temp = new Vector();
temp.add("test");
temp.add("test2");
temp.add("test3");

for (int i = 0; i < 2; i++) {
if (myVector == null) {
myVector = new Vector();
} else {
myVector.addAll(temp);
}
}
System.out.println(myVector);

}

That will print: "[test, test2, test3]"

And the second test:

static Vector myVector;
static Vector temp;

public static void main(String[] args) {
temp = new Vector();
temp.add("test");
temp.add("test2");
temp.add("test3");
myVector = new Vector();
myVector.add(0, "HELLO");

for (int i = 0; i < 2; i++) {
if (myVector == null) {
myVector = new Vector();
} else {
myVector.addAll(temp);
}
}
System.out.println(myVector);

}

Will print: "[HELLO, test, test2, test3, test, test2, test3]"


I believe this is basically the behavior you're looking for. In the case where the printlns don't execute, do you see the exception being thrown, and if so, what is it?
 

eLiu

Diamond Member
Jun 4, 2001
6,407
1
0
Originally posted by: OulOat
Next time: A) state what language. B) State which compiler. C) Read the documentation.

Assuming this is JAVA. Read the Java API

add(int index, Object element)
Inserts the specified element at the specified position in this Vector.

A string is not an object. Try myVector.add(0, (Object) "HELLO");

Are you sure a string is not an object...? Last time I checked it's not a primitive type, and I thought everything that is not primitive is an object. Besides, on Sun's documentation for "String," it says this:

public final class String
extends Object
implements Serializable, Comparable, CharSequence

Using Eclipse, I've been able to add Strings into Object-holders (i.e. containers, collections, etc) without a problem. (Of course, I had to type-cast back to String if I wanted to extract it).

-Eric
 

Bulldog13

Golden Member
Jul 18, 2002
1,655
1
81
Originally posted by: OulOat
Next time: A) state what language. B) State which compiler. C) Read the documentation.

Assuming this is JAVA. Read the Java API

add(int index, Object element)
Inserts the specified element at the specified position in this Vector.

A string is not an object. Try myVector.add(0, (Object) "HELLO");



A) Next time read the title before you open your mouth and reply.
B) The default one used by the java SDK...though for Vectors I couldn't imagine it means all that much...
C) I did, which is why I posted the problem....The java api in certain instances isn t exactly helpful. And yes I am aware of that page and use it.

"Tool"



Thanks for all the help...
rolleye.gif


Uh. String is an object....anymore advice you could give?
 

Bulldog13

Golden Member
Jul 18, 2002
1,655
1
81
Originally posted by: Kilrsat
A string is not an object. Try myVector.add(0, (Object) "HELLO");

Yes, a string is an object. So you can add it directly without casting it to the object class. I just slapped together some test code of my own.

Here's what I have:

static Vector myVector;
static Vector temp;

public static void main(String[] args) {
temp = new Vector();
temp.add("test");
temp.add("test2");
temp.add("test3");

for (int i = 0; i < 2; i++) {
if (myVector == null) {
myVector = new Vector();
} else {
myVector.addAll(temp);
}
}
System.out.println(myVector);

}

That will print: "[test, test2, test3]"

And the second test:

static Vector myVector;
static Vector temp;

public static void main(String[] args) {
temp = new Vector();
temp.add("test");
temp.add("test2");
temp.add("test3");
myVector = new Vector();
myVector.add(0, "HELLO");

for (int i = 0; i < 2; i++) {
if (myVector == null) {
myVector = new Vector();
} else {
myVector.addAll(temp);
}
}
System.out.println(myVector);

}

Will print: "[HELLO, test, test2, test3, test, test2, test3]"


I believe this is basically the behavior you're looking for. In the case where the printlns don't execute, do you see the exception being thrown, and if so, what is it?



That did it. Apparently, declaring them as new vectors did something I wasn't aware of. Care to explain why when they are re-declared as new Vectors it works?

And no, This is not some random homework assignment. It is part of a much larger webcrawler assignment.

Bah.

Thanks for the help.
 

Kilrsat

Golden Member
Jul 16, 2001
1,072
0
0
Originally posted by: Bulldog13
Originally posted by: Kilrsat
A string is not an object. Try myVector.add(0, (Object) "HELLO");

Yes, a string is an object. So you can add it directly without casting it to the object class. I just slapped together some test code of my own.

Here's what I have:

static Vector myVector;
static Vector temp;

public static void main(String[] args) {
temp = new Vector();
temp.add("test");
temp.add("test2");
temp.add("test3");

for (int i = 0; i < 2; i++) {
if (myVector == null) {
myVector = new Vector();
} else {
myVector.addAll(temp);
}
}
System.out.println(myVector);

}

That will print: "[test, test2, test3]"

And the second test:

static Vector myVector;
static Vector temp;

public static void main(String[] args) {
temp = new Vector();
temp.add("test");
temp.add("test2");
temp.add("test3");
myVector = new Vector();
myVector.add(0, "HELLO");

for (int i = 0; i < 2; i++) {
if (myVector == null) {
myVector = new Vector();
} else {
myVector.addAll(temp);
}
}
System.out.println(myVector);

}

Will print: "[HELLO, test, test2, test3, test, test2, test3]"


I believe this is basically the behavior you're looking for. In the case where the printlns don't execute, do you see the exception being thrown, and if so, what is it?



That did it. Apparently, declaring them as new vectors did something I wasn't aware of. Care to explain why when they are re-declared as new Vectors it works?

And no, This is not some random homework assignment. It is part of a much larger webcrawler assignment.

Bah.

Thanks for the help.

You need to instantiate the instance of the vector object (call the constructor, aka new Vector()) before you can use the non-static methods such as add(). Your program was crashing on "myVector.add(0, "HELLO");" before because you had not created the object associated with myVector at that point in time. Thus you were trying to call a non-static method from a pointer that references a null object. (If you had the actual exception, its very likely that it would have been a NullPointerException.)
 

screw3d

Diamond Member
Nov 6, 2001
6,906
1
76
Yep. Whenever you create a Java object, you'll need to instantiate is by declaring "SomeObject newObject = new SomeObject(parameters)".
 

Bulldog13

Golden Member
Jul 18, 2002
1,655
1
81
One more question...

Assuming I have a vector A with 1, 2, 3, 4, 5, 1, 2, 6.

What would the code for a function be to loop through and pull out dupes.

So all I would be left with is 1, 2, 3, 4, 5, 6.

I tried this :


public Vector checkForDupes (Vector v) {

for (int ctr=0; ctr<v.size(); ctr++) {
for (int ctr1 = 0; ctr1<v.size(); ctr1++)
if (v.elementAt(ctr) == v.elementAt(ctr1)){
System.out.println(v);
v.removeElementAt(ctr);
}
}

return v;
}


And yes I have tried mapping it to HashMaps and Hashtables. But for some reason I lose elements.


But that function didnt work.. I know it has something to do with my counters. Any help?
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
I don't know java but a hash is generally how you weed out duplicates, so you're on the right track.

Assuming java doesn't have anything builtin that does this.

If you need it to be sorted anyways, you could always sort it and then it would be easy to weed out consecutive duplicates. Less efficient, but I don't know if that matters to you.
 

Kilrsat

Golden Member
Jul 16, 2001
1,072
0
0
You make a few bad assumptions in your for loops. If you remove an item, the next item to check is at the same position as the last item checked, so you don't want to advance the counter.

This example:

public static void main(String[] args) {
Vector v = new Vector();
v.addElement(new Integer(1));
v.addElement(new Integer(2));
v.addElement(new Integer(3));
v.addElement(new Integer(4));
v.addElement(new Integer(5));
v.addElement(new Integer(1));
v.addElement(new Integer(2));
v.addElement(new Integer(6));
v = checkForDupes(v);
System.out.println(v);

}

public static Vector checkForDupes (Vector v) {
int pos = 0;

while (pos < v.size()) {
int pos2 = pos+1;
while (pos2 < v.size()) {
if (v.elementAt(pos).equals(v.elementAt(pos2))) {
System.out.println("removing - " + v);
v.removeElementAt(pos2);
} else {
pos2++;

}
}
pos++;
}
return v;
}

Prints:
removing - [1 , 2, 3, 4, 5, 1, 2, 6]
removing - [1, 2, 3, 4, 5, 2, 6]
[1, 2, 3, 4, 5, 6]

The first two printlns are from inside the checkforDupes method and the last is the final System.out.println from the main method. Now that I look at it again, the outer loop could be a for loop, but the while loop works just as well. You could do everything with the java.util.Hashtable class. Using a single loop, check if it contains the key, if it does, remove the item, if it doesn't, add to the hashtable. If you're working with vectors of thousands of items that would be a better route, but for under a thousand the time difference between that method and this one should not be noticable.
 

Punamo

Senior member
Jan 28, 2001
302
0
0
If you are ok with not writing the loop, try the following. The checkForDupes2 would get you a Vector without the returned Vector being sorted. The checkForDupesSorted gets you sorted results. Note though the sorting might return you different things depending on the objects in the vector. If you're storing your own objects (as opposed to predefined Java types such as String, Integer etc) then you'll have to implement some interfaces to get it to work.

The collections API is awesome once you know enough of its methods. Play around with it and you'll see how smart those API designers are.



import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;
import java.util.Vector;

public class Test {

public static void main(String[] args) {
Test t = new Test();
Vector vec = new Vector();
vec.add(new Integer(1));
vec.add(new Integer(2));
vec.add(new Integer(3));
vec.add(new Integer(4));
vec.add(new Integer(5));
vec.add(new Integer(6));
vec.add(new Integer(1));
vec.add(new Integer(2));
vec.add(new Integer(6));
Vector vec2 = t.checkForDupes2(vec);
Vector vec3 = t.checkForDupesSorted(vec);

System.out.println("Vec : " + vec);
System.out.println("Vec no dup : " + vec2);
System.out.println("Vec no dup sorted : " + vec3);


}

public Vector checkForDupes2(Vector v) {
Set s = new HashSet(v);
return new Vector(s) ;
}

public Vector checkForDupesSorted(Vector v) {
Set s = new TreeSet(v);
return new Vector(s) ;
}



public Vector checkForDupes(Vector v) {

for (int ctr = 0; ctr < v.size(); ctr++) {
for (int ctr1 = 0; ctr1 < v.size(); ctr1++)
if (v.elementAt(ctr) == v.elementAt(ctr1)) {
System.out.println(v);
v.removeElementAt(ctr);
}
}

return v;
}

}