Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is There a Way to Have an Arraylist Element Equal to a Class Constructor?

For an assignment on Arraylists, I am required to change the 1D array into an array list, meaning modifying the program from listName.length to listName.size(), etc.

I am having a problem with declaring my array list animals to a class method. I did this: animals.get(x) = new Dog(name, age); but I am getting an error where it says the left-hand side must be a variable. The same error is occurring on new Cat(name, age); and on new Bird(name, age);.

Now, I have tried creating a String variable and assigning to animals.get(x) and then assigning that variable to new Dog(name,age) which also does not work, as it wants me to change new Dog(name, age) to string (meaning change my variable name, lets say String string, to Dog string), and when I do that, I`m back at square one and it asks me to change my variable back to String.

import java.util.ArrayList;
import java.util.Collection;

public class Database {	
	ArrayList<String> animals = new ArrayList<String>();	
	Database (int s) {	
		animals.size();	
	}//end of Database(s)

    boolean addAnimal (int type, String name, int age) {
    	for (int x = 0; x < animals.size(); x++) {
    		if (animals.get(x) == null) {
    			if (type == 1) {
    				animals.get(x) = new Dog(name, age); 					
    			}//end of if	
    			else if (type == 2) {
    				animals.get(x) = new Cat(name, age);		
    			}//end of else if
    				else {
                              animals.get(x) = new Bird(name, age);
    				}//end of else
    				return true;				
    		}//end of outer if			
    	}//end of for loop		
    	return false;
    }//end of addAnimal(type, name, age)

	Animal removeAnimal (String name) {
		for (int x = 0; x < animals.size(); x++) {
			if (animals.get(x).equals(null)) {
				// If the spot in the array is null skip this index
			}//end of if
			else if (animals.get(x).equals(name)) {
				String found = animals.get(x);	
				animals.at(x) = null;
				System.out.print(found);
			}//end of else if
		}//end of for loop
		return null;
	}//end of removeAnimal(name)
}//end of class Database

Since I am modifying everything to fit array lists, I have to modify the method above as well when the user chooses to add an animal. There are three animals, Dog, Cat, and Bird, all of which have their own classes. Im expecting the error'the left-hand side must be a variable' to disappear, and Ive tried searching ways to fix it, but I can`t seem to find a solution similar to my problem.

EDIT UPDATE

I included the complete code for my Database class (the class that has the methods addAnimal and removeAnimal).

like image 504
Yashvi Shah Avatar asked Dec 14 '22 13:12

Yashvi Shah


1 Answers

Lets be careful here, ArrayList's size method doesn't work like an array's length:

new Object[10].length; // returns 10

List<Object> list = new ArrayList<>(10);
list.size(); // returns 0

list.addAll(Collections.nCopies(10, null));
list.size(); // returns 10

new ArrayList<>(Collections.nCopies(10, null)).size(); // returns 10

As you can see, ArrayLists don't store nulls by default, instead they simply don't store anything. That means there shouldn't be a need to loop through and finds nulls... You can simply add the value to the list.

boolean addAnimal(int type, String name, int age) {
    if (type == 1) {
        animals.add(new Dog(name, age));
    }
    else if (type == 2) {
        animals.add(new Cat(name, age));
    }
    else {
        animals.add(new Bird(name, age));
    }
    return true; // You can probably make this a void method now
}

P.S. I've seen in the comments your ArrayList is of type String ... Your animals aren't strings, so you can either convert them to strings using new Dog(name, age).toString(), or what I'd recommend is changing your ArrayList to be a common type (ie. ArrayList<Animal> animals or ArrayList<Object> animals).

like image 98
Adam Bates Avatar answered Jan 26 '23 01:01

Adam Bates