Solved

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

Posted on 2003-12-06
10
228 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
 
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

705 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now