Question

How to check if a specific name is in my array list i must return true...

How to check if a specific name is in my array list

i must return true if it is and false if it isnt

Homework Answers

Answer #1
// Answer is use the method contains
//contains() return true if element is in ArrayList
//else returns False

import java.util.ArrayList;
public class MyArrayList {
    public static void main(String[] args) {
        ArrayList<String> arr = new ArrayList<String>(4);

        arr.add("Ram");
        arr.add("Krishna");
        arr.add("Jon");
        arr.add("Don");

        //test the method
        System.out.println("Ram in array list? "+arr.contains("Ram"));
        System.out.println("Jennie in array list? "+arr.contains("Jennie"));
    }
}
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
[Java] I'm not sure how to implement the code. Please check my code at the bottom....
[Java] I'm not sure how to implement the code. Please check my code at the bottom. In this problem you will write several static methods to work with arrays and ArrayLists. Remember that a static method does not work on the instance variables of the class. All the data needed is provided in the parameters. Call the class Util. Notice how the methods are invoked in UtilTester. public static int min(int[] array) gets the minimum value in the array public...
IN JAVA: Write recursive method to return true if a given array has element equal to...
IN JAVA: Write recursive method to return true if a given array has element equal to employee emp, or returns false otherwise. Array can be empty or not. //PRECONDITION: Varible n denotes the number of occupied positions in the array and must be non-negative. Employee class has method getSalary() that returns employee's salary, // and it has method boolean equals(Employee emp) that accept an employee object and returns true if employee calling the equals method is equal as employee emp,...
Write recursive method to return true if a given array of integers, named numbers, with n...
Write recursive method to return true if a given array of integers, named numbers, with n occupied positions is sorted in ascending (increasing) order, or returns false otherwise. Array can be empty or not. //PRECONDITION: Varible n denotes the number of occupied positions in the array and must be non-negative. Employee class has method getSalary() that returns employee's salary. // An empty array and an array with single element in it, are sorted. Method isSortedRec must be recursive and returns...
Write recursive method to return true if a given array has element equal to employee emp,...
Write recursive method to return true if a given array has element equal to employee emp, or returns false otherwise. Array can be empty or not. //PRECONDITION: Varible n denotes the number of occupied positions in the array and must be non-negative. Employee class has method getSalary() that returns employee's salary, // and it has method boolean equals(Employee emp) that accept an employee object and returns true if employee calling the equals method is equal as employee emp, and returns...
Assignment 1 - ITSC 2214 I have to complete the array list for a shopping list...
Assignment 1 - ITSC 2214 I have to complete the array list for a shopping list code and can't figure out how to complete it: Below is the code Complete the three empty methods (remove(), find(), and contains()) in the ShoppingListArrayList.java file. These methods are already implemented in the ShoppingListArray class. /////////////////////////////////////////////////////////////////////////////////////////////////////////// Grocery Class (If this helps) package Shopping; public class Grocery implements Comparable<Grocery> { private String name; private String category; private int aisle; private float price; private int quantity;...
State True or False. i) Binary search is used for searching in a sorted array. AND...
State True or False. i) Binary search is used for searching in a sorted array. AND ii) The time complexity of binary search is O(log n). A) True, False B) False, True C) False, False D) True, True Explain
How do I get this portion of my List Iterator code to work? This is a...
How do I get this portion of my List Iterator code to work? This is a portion of the code in the AddressBookApp that I need to work. Currently it stops at Person not found and if it makes it to the else it gives me this Exception in thread "main" java.lang.NullPointerException    at AddressBookApp.main(AddressBookApp.java:36) iter.reset(); Person findPerson = iter.findLastname("Duck"); if (findPerson == null) System.out.println("Person not found."); else findPerson.displayEntry(); findPerson = iter.findLastname("Duck"); if (findPerson == null) { System.out.println("Person not found.");...
If my return on assets ratio is 3, that means that every dollar I have invested...
If my return on assets ratio is 3, that means that every dollar I have invested in assets is creating $3 worth of income. True False
True or False 1. The check register list all accounts and their number. 2. Write check...
True or False 1. The check register list all accounts and their number. 2. Write check is associated with the Banking section of the Home Page. 3. To record a cash sale we must create a sales receipt then record the bank deposit. 4. When recording an Invoice, QuickBooks credits Accounts Receivable 5. The Enter Bill window is used to record services received but not yet paid. 6. An invoice is used to order goods. 7. We use the customer...
In this problem I want to return a grocery store dictionary with the name as the...
In this problem I want to return a grocery store dictionary with the name as the key and the price the value. The price must be less than my budget and the distance must be less than maxDistance. So the output for this problem should be {'Kroger': 2.79, 'Walgreens': 1.99, 'Sprouts': 3.45} . This is the code that I have, but it returns an IndexError. def friendsgiving(stores, budget, maxDistance):     if stores == [] and budget == 0 and maxDistance...