• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to access and add an element to a list of lists  RSS feed

 
Khusbu Sinha
Ranch Hand
Posts: 117
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having trouble in finding the difference of elements in a list of lists. I want to add elements from a String array of time-intervals into this list of lists such that each single element (i.e list) of this list of lists stores a pair of numbers representing the time-interval from the String array.
Following is my partial code for this operation. I get the following run time exception:
java.lang.IndexOutOfBoundsException: Index: 1, Size: 0




What's wrong in this code?
 
Khusbu Sinha
Ranch Hand
Posts: 117
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not comfortable with this syntax, particularly at the following lines:
Line 17 and 22 where I try to add the numeric values of the `timeSlots` in each individual list of `listOfIntegers`.
Line 25 where I try to find the difference between the elements stored in each individual list.

Is there something wrong in these lines?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37496
547
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This creates an empty list that has the ability to contain lists.


The first thing you need to do is loop and actually add the number of lists you want. Otherwise when you call get(), there isn't anything to get.

As for the syntax, you don't need the "chained" syntax. Instead you can write this. Clearer as to what is going on?
 
Khusbu Sinha
Ranch Hand
Posts: 117
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks I got it.
 
Khusbu Sinha
Ranch Hand
Posts: 117
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried implementing this code once again. I aim to print a list of lists containing entry and exit times in the 24 hour decimal format from the "AM"-"PM" String format input by the user as a String array like this:
6AM#8AM
11AM#1PM
7AM#8PM
7AM#8AM
10AM#12PM
12PM#4PM
1PM#4PM
8AM#9AM

I declared the individual lists inside the for loop and assigned them values inside the loop but again got the following run time exception from my code:
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0




 
Khusbu Sinha
Ranch Hand
Posts: 117
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for line 7. I edited it:

 
Khusbu Sinha
Ranch Hand
Posts: 117
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But the same run-time exception continues
java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please cut and paste the entire error message into a post.
 
Khusbu Sinha
Ranch Hand
Posts: 117
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.rangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at TimeSchedule.Timein24hourFormat(TimeSchedule.java:14)
at TimeSchedule.main(TimeSchedule.java:49)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)
>
 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You've created the 'scheduledTime' List but you haven't put anything in it yet, such as a List<Integer>. When you call scheduleTime.get(anything) it will return index out of bounds.
 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To create scheduledTime you have
Now, to populate scheduledTime with the first entry you'll need
Then you can do
which will return an empty List<Integer>.
 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For your input you might have "6AM#8AM". You are splitting it on "#" which gives you "6AM" and  "8AM". Now you have to break those into two so that you have hours, an integer, and "AM" or "PM". You shouldn't be using contains() to do this. Seeing as how "AM" and "PM" are both two characters long, you should be able to divide the string using substring().

edit: Well, you could use contains() to see if it has "AM" or "PM" but you'd still need substring() for the hours.
 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm assuming that "12AM" should result in "0" hours.
 
Khusbu Sinha
Ranch Hand
Posts: 117
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. This helped me get rid of the exception. However, I do have one more query. Are there suitable in-built methods for checking parts of a String? For instance, the line:
.
This fails to check the value "3AM". I wish to checkif the value has "AM" in it or not.
 
Khusbu Sinha
Ranch Hand
Posts: 117
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I posted it without reading your recent post
 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Khusbu Sinha wrote:Thanks. This helped me get rid of the exception. However, I do have one more query. Are there suitable in-built methods for checking parts of a String? For instance, the line:
.
This fails to check the value "3AM". I wish to checkif the value has "AM" in it or not.


This would break out the hours and am/pm into separate strings. Then you can parseInt(hours) and "AM".equals(ampm).
 
Khusbu Sinha
Ranch Hand
Posts: 117
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much for this simplified version.
 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So far you haven't taken an object oriented approach. You have a list of lists which is awkward and cumbersome. What you really need is a list of time spans, i.e. begin/end time pairs. If you created a TimeSpan class then your scheduledTime would be declared as

A TimeSpan class might look something like this:

One of the benefits of this is that all the parsing and 24-hour clock adjustments are encapsulated in the constructor which simplifies your application.
 
Dana Ucaed
Ranch Hand
Posts: 406
6
Netbeans IDE Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the variable scheduledTime you didn't add nothing.

 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps you could re-post your code in its current form before continuing this thread.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!