Author Topic: override list add method?  (Read 1509 times)

ober

  • Ashton Shagger
  • Ass Wipe
  • Posts: 14310
  • Karma: +73/-790
  • mini-ober is taking over
    • Windy Hill Web Solutions
override list add method?
« on: July 15, 2011, 02:37:26 PM »
In the code below, StringUtils is a custom object we have to do certain operations.  All I'm trying to do is take a delimited string, split it up into a list, add a new element to the List before dumping it back into a delimited string.

Code: [Select]
List<String> reportList = StringUtils.delimSplitToList(reports, "|");
reportList.add(repID + "-0");
reports = StringUtils.join(reportList, "|");

I'm getting an exception:
Quote
null
java.lang.UnsupportedOperationException
   at java.util.AbstractList.add(AbstractList.java:151)
   at java.util.AbstractList.add(AbstractList.java:89)

What I've read is that the add method needs to be overridden.  How do I do that?  Or is there a better way to do what I want to accomplish?

webwhy

  • Jackass IV
  • Posts: 608
  • Karma: +15/-10
Re: override list add method?
« Reply #1 on: July 15, 2011, 03:12:57 PM »
Where is the exception being raised?  it's tough to tell from the snippet you posted.  is it being raised from your "add" call or from within the delimSplitToList method.

it's confusing to me why you even get that exception...is it possible there's a bug in the StringUtils library?  the only way i can think of this happening is that there's a custom list implementation in that library that inherits from AbstractList, but doesn't override the add method.

can you post the full stack trace?  Did you write the StringUtils class? 

just a thought:

if the format of the delimited string, reports, is guaranteed, might be simpler to just modify the delimited string in a StringBuilder instead of splitting, adding, then joining.

Code: [Select]
StringBuilder reportBuilder = new StringBuilder(reports);
reportBuilder.append("|").append(repID).append("-0");
reports = reportBuilder.toString();

ober

  • Ashton Shagger
  • Ass Wipe
  • Posts: 14310
  • Karma: +73/-790
  • mini-ober is taking over
    • Windy Hill Web Solutions
Re: override list add method?
« Reply #2 on: July 15, 2011, 03:22:50 PM »
Sorry, the exception is being triggered on the add call.

The StringUtils class is a custom class someone here wrote that gives us some basic manipulation tools that don't exist in the java core.

I might try the StringBuilder method.  The format is guaranteed since I'm building the entire thing and I have full control over it.

I think I might have figured out the problem that was causing the exception, but now I have another problem related to another class... gah.

hans

  • Guitar Addict
  • Jackass In Charge
  • Posts: 3523
  • Karma: +46/-18
Re: override list add method?
« Reply #3 on: July 18, 2011, 11:08:52 AM »
Why not just append what you want to the existing string? If it's the last element, seems like a lot of overhead to put add and put back if it's just going to get stuck on the end.
This signature intentionally left blank.

ober

  • Ashton Shagger
  • Ass Wipe
  • Posts: 14310
  • Karma: +73/-790
  • mini-ober is taking over
    • Windy Hill Web Solutions
Re: override list add method?
« Reply #4 on: July 18, 2011, 11:17:13 AM »
Well, for the one case, yes.  But another similar case requires me to loop through the elements to make sure the existing property that I'm trying to add isn't already there (or needs to be modified).  Hence, I break it up, loop through to remove it, then add it back in.