?
Solved

How to extract from a vector and add them to another vector?

Posted on 2003-12-06
10
Medium Priority
?
243 Views
Last Modified: 2010-03-31

Hi every one..
Could you please help me with my question..

-I have a class called Customer which has four variables (int id, String name, String city, String phone)

- I have another class called TestCustomer that has a main method..


Now in this class I have to:

1- create five customers and add them to a vector called "Customers"

2- create another Vector called "CustomerNames" and extract the 5 customers' names from the "Customers" vector and add them to the "CustomerNames" vectors..


My questions are:

1- when I create the five customers do I have to specify the values of the attributes?

2- How can I extract from a vector and add it to another one..


thanks in advance,,
waiting for your answers
0
Comment
Question by:SEstudent
  • 5
  • 3
  • 2
10 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 620 total points
ID: 9888821
>>1- when I create the five customers do I have to specify the values of the attributes?

Usually, yes and it's good to do so where possible.

>>2- How can I extract from a vector and add it to another one..

Iterate the Customers vector and add to the customer names one

Iterator iter = customers.iterator();
while(iter.hasNext()) {
   Customer cust = (Customer)iter.next();
   customerNames.add(cust.getName());
}

Note that Customer should have a getName method that returns a String. The 'customerNames' variable in the example is your other Vector

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9888828
...and of course 'customers' is the Vector of Customer
0
 

Author Comment

by:SEstudent
ID: 9888890


thanks alot for your help,,now it work but I want to ensure that it is working in the right way


now will this be OK


=========

import javax.swing.*;
import java.util.*;

public class TestCustomer {
  public static void main(String[] args) {
    Customer c1, c2, c3, c4, c5;
    Vector customers = new Vector();
    Vector customerNames = new Vector();
    c1 = new Customer(678, "John", "Dubai", "7658888");
    c2 = new Customer(888, "Bill", "Sharjah", "8879990");
    c3 = new Customer(223, "Ed", "Al-Ain", "7455668");
    c4 = new Customer(459, "Steve", "Abu Dhabi", "7763454");
    c5 = new Customer(142, "Harry", "RAK", "6678333");
    customers.add(c1);
    customers.add(c2);
    customers.add(c3);
    customers.add(c4);
    customers.add(c5);

    Iterator iter = customers.iterator();
while(iter.hasNext()) {
   Customer cust = (Customer)iter.next();
   customerNames.add(cust.getName());
}

 JOptionPane.showMessageDialog(null, "vector v: \n" +       customers.toString());
 JOptionPane.showMessageDialog(null, "vector vn: \n" +       customerNames.toString());

=================
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:CEHJ
ID: 9888913
That's OK. Of course, unless there's some specific reason for keeping those Customer references, you can do:

customers.add(new Customer(678, "John", "Dubai", "7658888"));

etc.
0
 

Author Comment

by:SEstudent
ID: 9888944

oh,,thanks alot..sorry for disturbing you..
but I have another question..

the last point (after creating another vector called CustomerID)  is that I have to move all the work to a method called "CreateCustomers" and call this method from the main method..could you please help me with that?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9888968
OK - but you must do your assignment yourself after this ;-)


In main:

TestCustomer tc = new TestCustomer();
tc.createCustomers();

...
// constructor

public TestCustomer() {
  customerNames = new Vector();
  customerIds = new Vector();
  customers = new Vector();
}

public void createCustomers() {
  // fill the Vectors
}
0
 

Author Comment

by:SEstudent
ID: 9888983

this helped alot,,

thanks alot :)
0
 
LVL 3

Expert Comment

by:monkesdb
ID: 9888991
you really want the customers customerNames and customerIDs to be member variables

TestCustomer {

    Vector customers = new Vector();
    Vector customerNames  = new Vector();
    Vector customerIDs = new Vector();


//you then want to use these in your method...

    private void CreateCustomers(/* pass in anything you use to create the data */)
    {
        //put your code in here
    }

    public static void main(String args[])
    {
         CreateCustomers();
    }

}
0
 
LVL 3

Expert Comment

by:monkesdb
ID: 9889013
if you want the CreateCustomers method to be inside a different class you will need to pass the Vectors into it as parameters.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9889055
8-)

Incidentally, method and variable names should start with a small letter, class name with a capital
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses
Course of the Month13 days, 10 hours left to enroll

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question