Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Beginning Java
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Tim Cooke
paul wheaton
Jeanne Boyarsky
Ron McLeod
Sheriffs:
Paul Clapham
Liutauras Vilda
Devaka Cooray
Saloon Keepers:
Tim Holloway
Roland Mueller
Bartenders:
Forum:
Beginning Java
Collection program not working as expected
shuba gopal
Ranch Hand
Posts: 87
1
posted 13 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hi the following program is from Deitel with minor modifications. The method removeColors is just as it is in the book. The colors BLUE,RED,GREEN are expected to be removed but they are still present after the method runs.
import java.util.*; public class collTest1{ private static final String [] colors = {"CHARTREUSE","BLUE","RED","GREEN","CYAN"}; private static final String [] removeColors = {"BLUE","RED","GREEN"}; public collTest1(){ List <String> list = new ArrayList<String>(); List <String> removeList = new ArrayList<String>(); for(String color:removeColors) removeList.add(color); for(String color:colors) list.add(color); System.out.println("ArrayList: "); for(int count = 0;count < list.size();count++) System.out.printf("%s ",list.get(count)); for(int count = 0;count < removeList.size();count++) System.out.printf("\n%s ",removeList.get(count)); removeColors(list,removeList); System.out.println("\n\nArrayList after calling removeColors "); for(String s:colors) System.out.printf("%s ", s); } private void removeColors( Collection< String > collection1, Collection< String > collection2 ) { // get iterator Iterator< String > iterator = collection1.iterator(); // loop while collection has items while ( iterator.hasNext() ) if ( collection2.contains( iterator.next() ) ) iterator.remove(); // remove current Color } // end method removeColors public static void main(String [] args){ collTest1 c = new collTest1(); } }[code=java]
shuba gopal
Ranch Hand
Posts: 87
1
posted 13 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hi I made some changes to the program and it worked. The SOP after removeColors() had to be modified. Thanks
import java.util.*; public class collTest1{ private static final String [] colors = {"CHARTREUSE","BLUE","RED","GREEN","CYAN"}; private static final String [] removeColors = {"BLUE","RED","GREEN"}; public collTest1(){ List <String> list = new ArrayList<String>(); List <String> removeList = new ArrayList<String>(); for(String color:removeColors) removeList.add(color); for(String color:colors) list.add(color); System.out.println("ArrayList: "); for(int count = 0;count < list.size();count++) System.out.printf("%s ",list.get(count)); for(int count = 0;count < removeList.size();count++) System.out.printf("\n%s ",removeList.get(count)); removeColors(list,removeList); System.out.println("\n\nArrayList after calling removeColors "); for(String color:list) System.out.printf("%s ", color); } private void removeColors( Collection< String > collection1, Collection< String > collection2 ) { // get iterator Iterator< String > iterator = collection1.iterator(); // loop while collection has items while ( iterator.hasNext() ) if ( collection2.contains( iterator.next() ) ) iterator.remove(); // remove current Color } // end method removeColors public static void main(String [] args){ collTest1 c = new collTest1(); } } [code=java]
I panicked because its Collection. Thanks
What? What, what, what? What what tiny ad:
New web page for Paul's Rocket Mass Heaters movies
https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Enum Type inhteritance
question on enum
AWT Event look for help
Struts 2 checkboxlist error - list key could not be resolved
problem with updating Panel!
More...