0

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?

3
  • 2
    Calling the add() method will return a boolean. If you want to create just an ArrayList object, you just have to write new ArrayList<Integer>(). Commented Nov 24, 2016 at 19:34
  • @LoganKulinski: If you have an answer next time, feel encouraged to write out an answer. No sense in leaving knowledge like that in comments. Commented Nov 24, 2016 at 19:36
  • @Makoto Will do, thank you. Commented Nov 24, 2016 at 19:37

2 Answers 2

3

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.
Sign up to request clarification or add additional context in comments.

Comments

1

While Makoto's answer is correct and explains why you are getting this error, and gives you sensible advice to create your ArrayList before you use it, you might want to know if there is any other way you can make your code more succinct.

Unlike more recent languages, such as Groovy or Kotlin, Java unfortunately does not have List literals as a language syntax. There have been some attempts to hack around this limitation; what you may have been attempting with your code is the double brace initialization idiom:

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

Although this may look cute, it does have its drawbacks, as described in the above link.

Do you really need to pass actual ArrayLists to your constructor? Why not make it take Lists, which will make it more flexible:

public PageBreak(String sheetName, List<Integer> rowPageBreaks, List<Integer> columnPageBreaks)
{
    ...
}

Then you have the freedom to pass it ArrayLists, as before, or any other kind of List:

pageBreaks.add(new PageBreak(teamResultSheet.getName(),
               Arrays.asList(teamResultSheet.getRowPageBreak()),
               null);

This looks more succinct and doesn't have the former's drawbacks; however, the list is fixed-size and immutable.

If you only want a single-element list, it's more efficient to use the following:

pageBreaks.add(new PageBreak(teamResultSheet.getName(),
               Collections.singletonList(teamResultSheet.getRowPageBreak()),
               null);

1 Comment

I avoided the double curly brace initializer intentionally since it makes the code less readable.

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.