Results 1 to 2 of 2

Thread: vector problem

  1. #1
    mambo_jumbo is offline Member
    Join Date
    Nov 2007
    Posts
    1
    Rep Power
    0

    Default vector problem

    i have two vectors a and b.
    i need to write elements from vector b that are at the same index as equal elements in vector a.
    if in vector a elements a(0) and a(5) are equal i write b(0) and b(5) into another vector.

    for (int i=0, n=a.size(); i<n; i++){
    onenode = (String)a.get(i);
    if (!visited.contains((String)a.get(i))){
    for (int j=0, m=a.size(); j<m; j++){
    secondnode = (String) a.get(j);
    if (secondnode.equals(onenode)){
    selectednets.add((String)b.get(j));
    }
    }
    visited.add(onenode);
    }
    // selectednets.removeAllElements();
    }
    if i uncoment vector clear i get empty vector and if i leave it all elements of b are stored in the selectednets vector.
    can anybody explane what am i doing wrong?

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    import java.util.*;
    
    public class TransferTest {
        public static void main(String[] args) {
            String[] aItems = {
                "Bob", "Sue", "John", "Salley", "Vincent", "John", "Bob", "Sue"
            };
            String[] bItems = {
                "Bob", "Jessica", "John", "Philip", "Salley", "Vincent", "Bob", "Ed"
            };
            Vector<String> a = new Vector<String>(Arrays.asList(aItems));
            Vector<String> b = new Vector<String>(Arrays.asList(bItems));
            Vector<String> selectednets = new Vector<String>();
    
            // elements from vector b that are at the same index
            // as equal elements in vector a
            for (int i=0, n=a.size(); i<n; i++){
                String onenode = (String)a.get(i);
                if (!selectednets.contains(onenode)){
                    String secondnode = (String) b.get(i);
                    if (secondnode.equals(onenode)){
                        selectednets.add(secondnode);
                    }
                }
            }
            System.out.println(selectednets);
            selectednets.removeAllElements();
    
            // Identify pairs of equal-value elements in a that occur in b 
            // at the same indices and copy the element into selectednets.
            // Only the first occurrence of duplicates will be checked.
            for (int i=0, n=a.size(); i<n; i++){
                String onenode = (String)a.get(i);
                for(int j = i+1; j < n; j++) {
                    String element = (String)a.get(j);
                    if(onenode.equals(element)) {
                        // Found duplicate value.
                        if(!selectednets.contains(onenode)){
                            String secondnode = (String) b.get(i);
                            if(secondnode.equals((String)b.get(j)) &&
                               onenode.equals(secondnode)) {
                                selectednets.add(secondnode);
                                System.out.println("Found " + onenode + " in a at " +
                                                   "indices " + i + " and " + j +
                                                   " and the matching\nvalue " +
                                                    secondnode + " in b at indices " +
                                                    i + " and " + j + ".");
                            }
                        }
                    }
                }
            }
            System.out.println(selectednets);
        }
    }

Similar Threads

  1. Vector create
    By Warren in forum New To Java
    Replies: 4
    Last Post: 03-02-2010, 03:42 AM
  2. Vector problem
    By Ace_Of_John in forum New To Java
    Replies: 1
    Last Post: 01-27-2008, 09:53 PM
  3. Vector help
    By king_arthur in forum New To Java
    Replies: 3
    Last Post: 01-22-2008, 08:33 PM
  4. array vs Vector
    By paty in forum New To Java
    Replies: 1
    Last Post: 08-02-2007, 08:07 PM
  5. Replies: 1
    Last Post: 07-16-2007, 05:31 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •