0

So, I have an 2D ArrayList thanks to this question: How to create a Multidimensional ArrayList in Java? And, now I have it inside a 2D array, thanks to this question: Convert ArrayList into 2D array containing varying lengths of arrays

I need my ArrayList to have a "blocked width size" (bws), that is, when the size of the ArrayList reaches that number (bws), it "breaks" the ArrayList line:

However, I have no ideia how to do it :(.

EDIT:

So, what should I do?

Community
  • 1
  • 1
A Cat
  • 629
  • 7
  • 20
  • What exactly is your question? – NPE Sep 27 '14 at 18:20
  • Question edited at the last line. – A Cat Sep 27 '14 at 18:21
  • 3
    I don't mean to be rude, but I think what you should do is write some code. Then show it to us and explain which part you're having difficulty with. Then we'll be able to help you make progress. – NPE Sep 27 '14 at 18:22
  • You aren't been rude :D. I just can't think of anything in about 2 days stuck on this. – A Cat Sep 27 '14 at 18:24
  • 3
    @CrazyProgrammer Even if you're not able to write actual code on this problem, I strongly suggest that you write pseudocode that will describe how you would mentally approach doing such an operation step-by-step. We can then help convert it to Java. – nanofarad Sep 27 '14 at 18:24

1 Answers1

1

You did not specify much about the interface, so I'll just define some basics from which you could go on and develop more:

  • The list with blocked width should be able to add elements

  • One should be able to access an arbitrary row in the list

You could go on and define an iterator, implement various collection interfaces and whatever you want.

Note that any client code should not be concerned about implementation details. Carefully think about the interface of your list with blocked width. The interface tells the client everything it needs to now. How the interface is implemented is solely the concern of the implementing class. That said, you can easily store your two-dimensional list with blocked-width as a single, plain java.util.List. But hide that list from your clients.

Here's the code I propose:

public class BlockedWidthList<T> {

    public static void main(String[] args) {
        BlockedWidthList<Integer> myList = new BlockedWidthList<Integer>(6);
        for(int i = 0; i < 20; i++)
            myList.add(i);
        for(Integer entry : myList.getRow(2))
            System.out.println(entry);
    }

    /** @throws java.lang.IllegalArgumentException if 'blockedWidthSize' is less than or equal to zero */
    public BlockedWidthList(int blockedWidthSize){
        if(blockedWidthSize<1)
            throw new IllegalArgumentException("Width must be a positive number");
        this.blockedWidthSize = blockedWidthSize;
    }

    public final int blockedWidthSize;

    private List<T> internal = new ArrayList<T>();

    public void add(T elem){
        internal.add(elem);
    }

    /** Access a row in the blocked width list by a row index. Indexing starts at 0
      *
      * @return A list containing up to 'blockedWidthSize' elements
      * @throws java.lang.IndexOutOfBoundsException if the row does not exist
      */
    public List<T> getRow(int rowIndex){
        if (rowIndex < 0) throw new IndexOutOfBoundsException("Negative row");
        int startIdx = rowIndex * blockedWidthSize;
        return internal.subList(startIdx, startIdx + blockedWidthSize);
    }
}

You can see that, under the hood, this class only uses a list and an integer. Add simply forwards the call to the underlying list.

The method you are probably interested in is getRow. Let's think about where the ith row can be found in the list.

  • The first row is always at index 0, since we are appending elements at the end.

  • The second row starts after removing blockWidthSize number of elements. Since we are 0 indexed, the second row starts at index blockWidthSize

  • If you think about it, the first element of the ith row is at index i * blockWidthSize.

We then simply return the subList starting at the computed index and ending blockedWidthSize elements later.

Edit: Obviously, this interface is fairly useless: A client can not know whether he can access a certain row unless he either knows all elements that were put into the list, or if he catches the exception thrown when accessing an invalid row. This means, in the minimum you should add a method to the interface that exposes information about the size of the list. This could simply be the number of elements (i.e., forwarding a call to internal.size(), or the number of rows (which requirs some computations, but is not too hard to do)

Kulu Limpa
  • 3,501
  • 1
  • 21
  • 31