0

I have a 2D ArrayList

List<List<String>> transitionTable = new ArrayList<List<String>>()

If I'm correct, adding "rows" to this would be (correct me if I'm wrong)

transitionTable.add(new ArrayList<String>())

How would I go about adding the equivalent of a column? Use a for loop something like this?:

for (int i = 0; i < transitionTable.get(0).size(); i++)
{
    transitionTable.get(i).add("something");    
}

edit: Now having trouble figuring out why it stops adding after a certain point, it's always at index i = anything (depending on how many times I've iterated the loop" j = 1 (always)

I forgot to mention I'm aiming for a nxn 2d arraylist (a square basically)

This is my code so far:

  transitionTable.add(new ArrayList<String>());   //Adds a new row
    if (transitionTable.size() == 1)
    {
        transitionTable.get(0).add("NULL");     //Adds a new column.
    }

    else 
    {
        for (int i = 0; i < transitionTable.get(0).size(); i++)
        {
            transitionTable.get(i).add("NULL");             //Adds a new column (needs to iterate for each row to create something like a column)

        }
    }
user2908849
  • 77
  • 2
  • 9
  • You probably can't use the `size()` like that. Until you add `String`(s) it has a size() of 0. – Elliott Frisch Nov 11 '14 at 19:03
  • 14
    Don't ask -- try. You've got a computer programming laboratory at your fingertips, so use it. Experiment, play, write code, run it, change it, push it to the limit and then go beyond, find out what works what doesn't work. Trust me, you're not going to blow up your computer, you're not going to bring on doom and damnation from the effort. For simple questions that can be answered by testing, don't ask us here -- find out for yourself. That's what learning and what programming is all about! – Hovercraft Full Of Eels Nov 11 '14 at 19:03
  • 3
    @Hovercraft Full Of Eels +1 for the answer; to expand on it yes you make full use of the debugger, it shows a variety of things including how your arraylist is structured - There you will find your answers! – horHAY Nov 11 '14 at 19:05
  • @ElliotFrisch Ah, yeah I forgot to copy the if statement that stops the loop from running when size is 0. – user2908849 Nov 11 '14 at 19:05
  • 1
    You don't need an `if` like that. But you've got almost exactly the right idea. You've made one small mistake (which you'll discover as soon as you start testing); and there's also a shorter and more intuitive way of writing the answer. – Dawood ibn Kareem Nov 11 '14 at 19:06
  • If you forgot something, use the `edit` link below your question and add it. – RealSkeptic Nov 11 '14 at 19:09
  • Tried testing a few things, and I check the 2d arraylist contents and it seems the loop always stops adding at i=depends on size j=1, can't figure out why! – user2908849 Nov 11 '14 at 20:15
  • You should update your question if you have new code. – Tom Nov 11 '14 at 20:18
  • Yep, the new code is has been edited in or am I doing it wrong? Sorry stackoverflow newbie here! – user2908849 Nov 11 '14 at 20:21
  • No, it is ok. You've mention `j=1`, that is why I thought you have new code: there is no `j` variable there. But there is this error: `transitionTable.get(0).size()` is the size of the "inner" `List` (the current count of "columns"). You may use `transitionTable.size()` instead. – Tom Nov 11 '14 at 20:27

3 Answers3

0

I would suggest using a temporary "reference" ArrayList, like this:

List<List<String>> templist = new ArrayList<List<String>>();
for(List<String> list1 : transitionTable) {
    list1.add("String whatever");
    templist.add(list1);
}
transitionTable = templist;

Edit: Accidentally posted prematurely.

mirvine
  • 126
  • 1
  • 2
  • 14
  • 1
    It's not a temporary ArrayList. It's a temporary List reference variable that refers to the actual ArrayList inside the table, one row at a time. The distinction is very important - no copying is done or needed. – RealSkeptic Nov 11 '14 at 19:09
  • And what is the purpose of this temporary List? Remove that `templist` completely and you have the exact same functionality. – Tom Nov 11 '14 at 19:21
  • @Tom because you can't add it directly to transitionTable or you would have duplicates. `templist` serves as a temporary placeholder to remove them from transitionTable completely, using less code than removing it manually. – mirvine Nov 11 '14 at 19:31
  • Btw: `new ArrayList>;` should be `new ArrayList>();`, or if you're using Java 7 `ArrayList<>();` :). – Tom Nov 11 '14 at 19:44
  • Why should this create duplicates? Fetch every list from transitionTable and add "String whatever" to them. Now where does the duplicates came from? If you call `transitionTable.add(list1);` in that loop, yes, that will creates duplicates, but why should someone has this nonsense idea? ("re-post" to remove a lot of typos) – Tom Nov 11 '14 at 19:48
  • @Tom are you saying just use the for loop and add them there? http://stackoverflow.com/questions/26783000/change-the-reference-in-a-java-list/26783356#26783356 – mirvine Nov 12 '14 at 14:06
  • @itrollin98 the code would look like this `for (List list1 : transitionTable) { list1.add("String whatever"); }`. This adds the new column, but it doesn't create duplicates of exsiting lists. This woulds happen if you call `transitionTalbe.add(list1);` in the loop, but as I said, no one should have this idea :D. – Tom Nov 12 '14 at 14:09
  • @itrollin98 about that link: this is a slightly different situation there (due to the immutability of strings). And btw: your code doesn't work. – Tom Nov 12 '14 at 14:14
0

At the moment, each iteration will add a value in the first column because they are empty;

After adding three arraylists;

transitionTable 
 - ArrayList[0] - []
 - ArrayList[1] - []
 - ArrayList[2] - []

After using your loop;

transitionTable 
 - ArrayList[0] - [something] 
 - ArrayList[1] - [something] 
 - ArrayList[2] - [something]

But when you add more items to each list, you then need to edit the position in the arraylist specifically! As David Wallace said, you are close, I hope the above bit of information helps!

horHAY
  • 788
  • 2
  • 10
  • 23
0

A Java List (and ArrayList is just one implementation) is a way to represent a list of things, such as:

  • Strings: 〖 "This" "That" "Another string" "Wow" 〗
  • Integers: 〖 15 208 -632 17 999 5 〗
  • Other objects, anything you can describe with a class, like Animal or Contact.

Just think of them as lists you have on paper. They have order (but they are not necessarily sorted), new items can be added, items can be removed, and you can access them by index or iterate on them in order.

So what exactly does a List of Lists look like? Basically, something like this:

〖 〖 "This", "That" 〗 〖 "Something" 〗 〖 〗 〖 "A" "Bunch" "Of" "Several" 〗〗

This is a list of four items. Each item is a list of strings. Note something important about it, though: the lists inside it are still lists, and they are independent of one another just like the numbers in the list of Integers. For this reason, each of the lists inside can have a different number of items.

This is important to remember if you want to use a list of lists to represent a table. Tables are objects that have the same number of items in each row. And you have the concept of a column. In Excel, you can copy a column together as one, to somewhere else.

Using a List of Lists doesn't give you the concept of equal-length-rows and separable columns. At least, not directly. If you use it to represent a table, you should probably wrap it in a class that makes sure that every time you extend one row, all the others are extended as well, and allows you to do things like return whole columns or delete whole columns.

It would do so by looping over the rows and performing the operations on each sub-list separately.

You can use a List of Lists as a table even without wrapping it as said, if you don't actually need all those operations and you just need a rudimentary implementation of a table as part of something else. You will still have to take care to fill and retrieve "columns" by looping over the rows and accessing the required column on each of them.

RealSkeptic
  • 33,993
  • 7
  • 53
  • 79