Well i'm doing a program to search the arraylist for the specific string.
friends = arraylist
currently my arraylist contains lots of information
etc of it
name: John number: 123
name: Bruce number: 242
currently my program kepts moves to the else statement, I hope someone could help me on this. Is there a way to search the arraylist without a complete match ?
for (int i = 0; i < friends.size(); i++)
{
if (friends.contains("Bruce"))
{
System.out.println(friends.get(i).toString());
}
else
{
System.out.println ("No Records Found");
}
}
Update:yes , its string
Copyright © 2024 QUIZLS.COM - All rights reserved.
Answers & Comments
Is your ArrayList of type String?
edit..
I think the 'contains' method is checking for a reference to an actual Object. So maybe you can try something like..
if (friends.get(i).equals("bruce"))...
(i.e. compare by value and not reference)
Also.. you shouldn't be using the 'contains' inside a loop -- The ArrayList contains method handles it's own iteration.
hi I had this for a lab. it may desire to no longer be executed wisely (i'm in basic terms a beginner) besides the indisputable fact that it works for me. of direction you may desire to alter your equipment and sophistication call equipment javaapplication16; import java.util.ArrayList; public type JavaApplication16 { public static void significant(String[] args) { ArrayLista0faef851b4294c6f2b94bb1cb2044 record = new ArrayLista0faef851b4294c6f2b94bb1cb2044(); record.upload(new Integer(50)); record.upload(new Integer(80)); record.upload(new Integer(seventy two)); record.upload(new Integer(32)); record.upload(new Integer(a hundred)); record.upload(new Integer(sixty six)); record.upload(new Integer(0)); gadget.out.println("Numerical type grades" + record); gadget.out.print("Corresponding letter grades"); for ( int i = 0; i < record.length(); i++) { ArrayList76a4e4f974fd895a0a2598c1cee28b4 score = new ArrayList76a4e4f974fd895a0a2598c1cee28b4(); int fee = record.get(i); if (fee>= ninety) { score.upload(new character ('A')); } else if (fee>= 80) { score.upload(new character ('B')); } else if (fee>= 70) { score.upload(new character ('C')); } else if (fee>= 60) { score.upload(new character ('D')); } else if (fee<60) { score.upload(new character ('F')); } gadget.out.print(score); } } }
Instead of using just "Bruce", try using a wild card. ie. "Bruce*"