Solved

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

Posted on 2003-12-06
10
232 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 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

 
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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
The viewer will learn how to implement Singleton Design Pattern in Java.
This video teaches viewers about errors in exception handling.

830 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