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

Posted on 2011-10-07
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
    LVL 20

    Assisted Solution

    by:chaitu chaitu
    SelectProducts .add(searchCriteria);
    LVL 86

    Expert Comment

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

    for(Product p : products) {
    	String prodName = p.getName();
    	if(searchCriteria.equals(prodName)) {
               // add to selected

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
    Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
    The viewer will learn how to implement Singleton Design Pattern in Java.
    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.

    779 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

    12 Experts available now in Live!

    Get 1:1 Help Now