I'm working on an assignment where I need to take "books" from the user(a title, an author, and ISBN number, all strings), and store these books somehow, and then do various things to these books using methods.
What I've chosen to do is store each "book" as an array with three values, one for the title, one for author, one for ISBN number. Then, I store that array inside an arraylist. where I hope to call upon each "book" later.
Unfortunately, the way I've chosen to go about this is to add a while loop so that the user can add as many books as they choose before moving on. But since its a loop, each time the user goes around, they essentially rewrite the same array(I.E. it has the same name) before pushing it back into the arraylist. After a lot of agonizing that was the only way I could think of doing it. Code below:
static int numofbooks = 0;//I think you can ignore these for this question static int checkedbooks = 0; static ArrayList<String[]> AllBooks = new ArrayList<String[]>(); //this arraylist is the "library" that all the "books" are stored in. It's a static so it can be referenced across every method public static void Add() { String user_answer = new String("yes");//this is for the while loop Scanner cont = new Scanner(System.in); Scanner bookscanner = new Scanner(System.in); while (user_answer.equalsIgnoreCase("yes")) { String[] Book = new String[3];//This is the "book" to be stored System.out.print("Enter book name: ");//the various detes Book[0] = bookscanner.nextLine(); System.out.print("Enter the author's name: "); Book[1] = bookscanner.nextLine(); System.out.print("Enter the ISBN: "); Book[2] = bookscanner.nextLine(); AllBooks.add(Book);//The "book" is pushed into the arraylist numofbooks++;//ignore System.out.print("Would you like to add another book? Type yes if so: "); user_answer = cont.nextLine();//if the user wants, they can do it again } } My question is that since I basically use the same array name every time, will it even be possible to call on each array again when I need them?
For example I will need to remove one or more books from the arraylist, will this be possible(or easy) with this system?
Sorry, I'm a major beginner.
I've tried various methods to verify that each array is being properly stored, such as by printing out the arraylist, however I've been unsuccessful. In one case, using an "Arrays.toString" I saw that two arrays were being stored at different memory locations(?) but I'm not sure how reliable this will be.
![screenshot of memory layout with an ArrayList of String[] instances](https://i.sstatic.net/3GrYBtMl.png)