0

I have a 2 dimensional array and fill it at start :

public static String [][] BooksList =
    {
        {"Coders at Work",null},
        {"Code Complete",null},
        {"The Mythical Man Month",null},
        {"Don’t Make Me Think, Revisited",null},
        {"The Pragmatic Programmer",null}
    };

I want to add more rows anytime. But array class doesn't allow increase the size of array. So i must use ArrayList like this :

ArrayList<List<String>> BooksList= new ArrayList<List<String>>();

But i don't know how to fill it like arrays.

13
  • 1
    Use Arrays.asList Commented Aug 12, 2017 at 15:03
  • @texasbruce That doesn't allow for increasing the size of the list either. Commented Aug 12, 2017 at 15:05
  • Have you taken a look at the ArrayList Javadoc? Is anything about it unclear? Commented Aug 12, 2017 at 15:06
  • The second dimension of your array is always null. Can you explain your need/use case ? It seems a xy problem. Commented Aug 12, 2017 at 15:08
  • @JoeC yes i saw them but i'm confusing .. need a simple example. Commented Aug 12, 2017 at 15:09

3 Answers 3

2

You can add items to the list like this:

BooksList.add(Arrays.asList("Cool book", null));
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you its worked ! and how i access the 2th value of 2th List of ArrayList ?
BooksList.get(1).get(1) Remember lists start at 0
How to add item to list of specific row ? like: BooksList.get(0).add("YES");
BooksList.get(0).add(int index, E element)
It doesn't work, i tried this : BooksList.get(0).add(0,"Yes"); also BooksList.get(0).add(1,"YES"); and BooksList.get(0).add(2,"YES");
2

According to your comment :

It is the simple library system that anytime can add book and every book also have name of person that borrowed it.

You should probably use a Map where the key is a book and the value is a List of borrower (as a book may probably have been booked by several people).
Using a map will ease the retrieval/modification of a specific book.
With array or List, when you want to access/change a specific book, you have to iterate the List until finding it.

You could so create a Map with books :

Map<Book, List<Borrower>> borrowersByBook = new HashMap<>();
borrowersByBook.put(new Book("Coders at Work"), new ArrayList<>());
borrowersByBook.put(new Book("Code Complete"), new ArrayList<>());
...

And add borrowers for books :

 List<Borrower> borrowers = borrowersByBook.get(new Book("Coders at Work"));
 borrowers.add(new Borrower("SaMi GiMiX", LocalDate.of(2017, 8, 12));
 borrowers.add(new Borrower("davidxxx", LocalDate.of(2017, 8, 12));

If you use a Book class instead of a String class to represent a book, you have to override equals()/hashcode() methods.

Comments

1

A two dimensional array can be represented with lists like this :

List<List<String>> BooksList = new ArrayList<>();
//adding elements
BooksList.add( Arrays.asList("ONE","TWO") );
//or like this
List<String> row = new ArrayList<>();
row.add("THREE");
row.add("FOUR");
BooksList.add(row);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.