|
-
Dec 14th, 2003, 01:43 AM
#1
Thread Starter
Member
Conditional add of vectors... **RESOLVED**
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:
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?
Last edited by IwishIcouldprog; Dec 14th, 2003 at 03:10 PM.
-
Dec 14th, 2003, 12:47 PM
#2
Dazed Member
Your Vector cannot be null since you are adding an element to it with the line myVector.add(0,"HELLO"); so its obvious that you already have a refrence to it. With the following code the else statement is always executed.
Code:
import java.util.*;
public class F{
public static void main(String[] args){
Vector v = new Vector();
v.addElement("Poo");
v.addElement("Poop");
if (v == null){
System.out.println("Vector equals null" + v);
}else {
System.out.println("Vector does not equal null" + v);
}
}
}
-
Dec 14th, 2003, 03:05 PM
#3
Thread Starter
Member
Thanks. I found the answer here.
http://forums.anandtech.com/messagev...GE=1&zb=619775
Which was essentially the same as yours. I was missing the new Vector() assignment.
Thought I got alot more guff there.
-
Dec 14th, 2003, 03:20 PM
#4
Dazed Member
Anytime.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|