Results 1 to 4 of 4

Thread: Conditional add of vectors... **RESOLVED**

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2002
    Posts
    45

    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.

  2. #2
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    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);
          }
       }
     }

  3. #3

    Thread Starter
    Member
    Join Date
    Sep 2002
    Posts
    45
    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.

  4. #4
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    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
  •  



Click Here to Expand Forum to Full Width