Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-17-2007, 08:02 PM
Member
 
Join Date: Nov 2007
Posts: 1
mambo_jumbo is on a distinguished road
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?
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-18-2007, 12:44 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
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); } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Vector problem Ace_Of_John New To Java 1 01-27-2008 10:53 PM
Vector help king_arthur New To Java 3 01-22-2008 09:33 PM
Vector create Warren New To Java 3 11-19-2007 10:13 PM
array vs Vector paty New To Java 1 08-02-2007 09:07 PM
Problem with vector, java.lang.ClassCastException paul New To Java 1 07-16-2007 06:31 PM


All times are GMT +3. The time now is 04:24 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org