[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2003-12-06
10
Medium Priority
?
240 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 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
Technology Partners: 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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

656 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