Solved

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

Posted on 2003-12-06
10
238 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
10 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 155 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
Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

 
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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

691 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