Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

accessing an array using .size() to define valid range  RSS feed

 
Ric Luper
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is a better way to write nextItem().

I have an object TaskModel that contains a list of TaskItems. A swing interface shows the contents of the current item. The current item is indicated by currentTaskId. Clicking on a "next" button calls nextItem().

I want nextItem() to increment the value of currentTaskId by 1. Unless it causes an out of bounds error.

I dont like how nextItem() is written because I belive that taskItems.size() uses to many resources and the error ( out of bounds )isnt handled well.

Am I worrying about nothing?

Should I use iterator()?

 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ArrayList.size() just reads a member variable -- far from "using too many resources", it's about as close to "free" as you can get.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!