Question

How do remove an object from list and add to another with indexes (java) Ex: I...

How do remove an object from list and add to another with indexes (java)

Ex: I have two separate list classes, Dogs and WalkedDogs, so once a dog from the Dog list has been walked, I want to remove that dog from the Dog list and at the same time then add it to the WalkedDogs list. By doing so via ui, so when main runs, the user is shown a list of Dogs, and chooses a dog with index (so can choose what dog they want to walk) then once specific index (dog) is chosen, user walks the dog and it is removed from the Dog list and added to the WalkedDogs list. I hope this isn't to confusing, thanks in advanced!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

If you have any queries regarding the program feel free to comment

Program

import java.util.ArrayList;

/*i have created Dogs and WalkedDogs class just for
* demonstration purpose, please change it accordingly*/

class Dogs{
   private String name;

   public Dogs(String name) {
       this.name = name;
   }

   @Override
   public String toString() {
       return name;
   }
}
class WalkedDogs{
   private String name;
  
   public WalkedDogs(String name) {
       this.name = name;
   }
  
   @Override
   public String toString() {
       return name;
   }
}

public class Test{
   public static void main(String[] args) {
       //created list of both classes
       ArrayList<Dogs> dogs = new ArrayList<>();
       ArrayList<WalkedDogs> walkedDogs = new ArrayList<>();
      
       //added items to dogs
       dogs.add(new Dogs("Charlie"));
       dogs.add(new Dogs("Oscar"));
       dogs.add(new Dogs("Max"));
       dogs.add(new Dogs("Olie"));
      
      
       int index = 3; //user want to walk index 3 dog
      
       walked(index, dogs, walkedDogs);//user has completed the walk
      
       //result after walk
       System.out.println("dogs: "+dogs);
       System.out.println("walkedDogs: "+walkedDogs);
   }

   private static void walked(int index, ArrayList<Dogs> dogs, ArrayList<WalkedDogs> walkedDogs) {
      
       String name = dogs.get(3).toString();//getting the items of dogs @index 3
      
       dogs.remove(index);//removing index 3 from dogs
      
       walkedDogs.add(new WalkedDogs(name));//adding it to the walkedDogs
   }
}

Output

<terminated Test (1) [Java Application] C:\Program Files\Java\jre1.8.0_211\bin\javaw.exe (26-Jul-2019, 12:06:40 pm) dogs: [Ch

Add a comment
Know the answer?
Add Answer to:
How do remove an object from list and add to another with indexes (java) Ex: I...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • The following is for java programming. the classes money date and array list are so I are are pre...

    The following is for java programming. the classes money date and array list are so I are are pre made to help with the coding so you can resuse them where applicable Question 3. (10 marks) Here are three incomplete Java classes that model students, staff, and faculty members at a university class Student [ private String lastName; private String firstName; private Address address; private String degreeProgram; private IDNumber studentNumber; // Constructors and methods omitted. class Staff private String lastName;...

  • Subject: Java Program You are writing a simple library checkout system to be used by a...

    Subject: Java Program You are writing a simple library checkout system to be used by a librarian. You will need the following classes: Patron - A patron has a first and last name, can borrow a book, return a book, and keeps track of the books they currently have checked out (an array or ArrayList of books). The first and last names can be updated and retrieved. However, a patron can only have up to 3 books checked out at...

  • JAVA: Already completed: MyList.java, MyAbstractList.java, MyArrayList.java, MyLinkedLink.java, MyStack.java, MyQueue.java. Need to complete: ReversePoem.java. This program has...

    JAVA: Already completed: MyList.java, MyAbstractList.java, MyArrayList.java, MyLinkedLink.java, MyStack.java, MyQueue.java. Need to complete: ReversePoem.java. This program has you display a pessimistic poem from a list of phrases. Next, this program has you reverse the phrases to find another more optimistic poem. Use the following algorithm. 1.   You are given a list of phrases each ending with a pound sign: ‘#’. 2.   Create a single String object from this list. 3.   Then, split the String of phrases into an array of phrases...

  • Hi all, I'm studying for a test for my Java class that coming up on Tuesday,...

    Hi all, I'm studying for a test for my Java class that coming up on Tuesday, and I would love it if someone could look over my answer. Can someone give me some feedback on the wrong one I get wrong? I really need to get it right for the upcoming test. Here my prep test: Which one statement is true" Question1 All objects that are eligible for garbage collection will be removed from the heap by the garbage collector....

  • Below is the code for the class shoppingList, I need to enhance it to accomplish the...

    Below is the code for the class shoppingList, I need to enhance it to accomplish the challenge level as stated in the description above. public class ShoppingList {   private java.util.Scanner scan; private String[] list; private int counter; public ShoppingList() { scan = new java.util.Scanner(System.in); list = new String[10]; counter = 0; } public boolean checkDuplicate(String item) { for(int i = 0; i < counter; i++) { if (list[i].equals(item)) return true; } return false; } public void printList() { System.out.println("Your shopping...

  • C programming A linked list is a linear data structure that allows us to add and remove items fro...

    c programming A linked list is a linear data structure that allows us to add and remove items from the list very quickly, by simply changing a few pointers. There are many different variations of linked lists. We have studied the doubly-linked, circular, with a dummy-header-node version of a linked list. In the class notes we studied several functions to manipulate a Linked List. For this assignment you must write the code for the following additional linked list functions: addFirst,...

  • I need help programming the main args. I am not sure what to do after I...

    I need help programming the main args. I am not sure what to do after I create an array list and scanner.h import java.util.ArrayList; import java.util.Scanner; public class Fantasy Football public static void main(string] args) ArrayList<String> availablePlayers - new ArrayList<String>0; addPlayers (avallablePlayers), Stringt roster; roster = new String[5]; Scanner player = new Scanner(System.in); System.out.println("Enter Player you would like on your tein: "); String playeri - player.nextLine(); public static int search(ArrayListString> array, String player) for (int i = 0; i <...

  • Question from Object-Oriented Data Structures Using Java 4th Edition Chapter 5 Question 30 Add the following...

    Question from Object-Oriented Data Structures Using Java 4th Edition Chapter 5 Question 30 Add the following methods to the LinkedCollection class, and create a test driver for each to show that they work correctly. Code each of these methods by accessing the internal variables of the LinkedCollection, not by calling the previously defined methods of the class.String toString() creates and returns a string that correctly represents the current collection. 1. Such a method could prove useful for testing and debugging...

  • Write a Java application program that plays a number guessing game with the user. In the...

    Write a Java application program that plays a number guessing game with the user. In the starter code that you are given to help you begin the project, you will find the following lines of code: Random generator = args.length == 0 ? new Random() :                    new Random(Integer.parseInt(args[0])); int secret = generator.nextInt(100); You must keep these lines of code in your program. If you delete these lines, or if you change thse lines in any way, then your program...

  • Remove srand(time(NULL)); from this C++ code so that it still finds random numbers correctly. Then, Write...

    Remove srand(time(NULL)); from this C++ code so that it still finds random numbers correctly. Then, Write a program that adds the following to the fixed code. • Add a function that will use the BubbleSort method to put the numbers in ascending order. – Send the function the array. – Send the function the size of the array. – The sorted array will be sent back through the parameter list, so the data type of the function will be void....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT