[Last Call] Learn how to a build a cloud-first strategyRegister Now


How do i add the matched element from one array into another array

Posted on 2011-10-07
Medium Priority
Last Modified: 2012-05-12
String searchCriteria = request.getSearchCriteria();

Product product1 = new Product();
            Product product2 = new Product();
ArrayList<Product> products = new ArrayList<Product>();
            for (int i=0; i<=products.size(); i++){
                  if (products.contains(searchCriteria)){
                        ArrayList<Product> SelectProducts = new ArrayList<SDProduct>();
                        ----- I'm trying to add the products.contains(searchCriteria) into selectProducts--------------
            SearchProductResult res = new SearchProductResult(SelectProducts, searchCriteria, showResult);
            return res;
Question by:Newton21205
  • 2
LVL 20

Assisted Solution

by:chaitu chaitu
chaitu chaitu earned 100 total points
ID: 36931553
SelectProducts .add(searchCriteria);
LVL 86

Expert Comment

ID: 36931666
>>  if (products.contains(searchCriteria)){

will never be true - the type of object in the Collection is Product and the criteria are of type String. You probably need to look at a field of Product
LVL 86

Accepted Solution

CEHJ earned 400 total points
ID: 36931679
for(Product p : products) {
	String prodName = p.getName();
	if(searchCriteria.equals(prodName)) {
           // add to selected

Open in new window


Featured Post

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!

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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 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 will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Suggested Courses
Course of the Month17 days, 20 hours left to enroll

829 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