Benoit Goderre Benoit Goderre - 10 days ago 5
Java Question

Array List initialization displayed as boolean

I am trying to create an object with the following constructor

public PageBreak(String sheetName, ArrayList<Integer> rowPageBreaks, ArrayList<Integer> columnPageBreaks)
{
this.sheetName = sheetName;
this.rowPageBreaks = rowPageBreaks;
this.columnPageBreaks = columnPageBreaks;
}


In another method, I do the following call:

pageBreaks.add(new PageBreak(teamResultSheet.getName(),
new ArrayList<Integer>().add(teamResultSheet.getRowPageBreak()), null));


I then get the following error message: The constructor PageBreak (String, boolean, null) is undefined.

How can I create an
ArrayList<Integer>
on the spot?

Answer

You're going to want to populate your list before you attempt to use it. Here's what's happening:

  • You instantiate a new ArrayList<Integer>.
  • You immediately call add on that list.
  • The result of add is boolean (and consequently always returns true).
  • The boolean result is what is interpreted, and not the list.
Comments