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

Splitting List<Integer> type to 2 Integers and 2 Integer[]  RSS feed

 
Stephen Murphyy
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a List of Integer Type I wish to break down. The first and last element to individual Integers and elements 2,5,8,11.... to an Integer [] and elements 3,6,9,12... to another Integer []

the following is a print of the initial List, error and code, any help would be appreciated, sure its something simple, just can't seem to figure it out

[20, 1, 91, 29, 2, 60, 65, 3, 61, 71, 4, 9, 60, 5, 79, 45, 6, 46, 71, 7, 19, 22, 8, 57, 97, 9, 8, 6, 10, 84, 91, 11, 20, 57, 12, 72, 60, 13, 32, 49, 14, 31, 89, 15, 28, 2, 16, 81, 30, 17, 55, 90, 18, 43, 25, 19, 100, 82, 20, 27, 19, 524]
Exception in thread "main"
java.lang.NullPointerException
at Items.main(Items.java:35)

 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

I think I'm going to need to see the rest of the code. Can you post it here?
 
Campbell Ritchie
Marshal
Posts: 55717
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

So which line are you getting the Exception on? The line numbers you have posted and the line number on the stack trace are different. I hope that doesn't mean you have a 35‑line main method.
 
Stephen Murphyy
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The complete code
 
Paweł Baczyński
Bartender
Posts: 2054
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lists are numbered starting with 0.
So you want to extract an element with index 0 to an int variable.
Then you want to extract an element at the end of the list to another int variable.
Then for the rest of the list you want to extract elements with indexes 2, 5, 8, 11... etc to an int array.
Then for you want to extract elements with indexes 3, 6, 9, 12... etc to another int array.
Am I correct to assume you want to ignore elements with indexes 1, 4, 7, 10... ?
 
Campbell Ritchie
Marshal
Posts: 55717
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Start by expunging the keyword static from your vocabulary. You may need it before the main method but avoid it everywhere else.

Then remember to initialise all fields. You did not initialise the arrays anywhere.
 
Stephen Murphyy
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes I want to ignore those elements.
Thanks for the Launcher Link, no more oversized main
Initialise and drop the static, thanks will do.
 
Campbell Ritchie
Marshal
Posts: 55717
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can iterate your List with its index and add the elements to another List/array/whatever depending on whether the index divides by 3.
 
Stephen Murphyy
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all the help, tidied my code up, all working.
 
Campbell Ritchie
Marshal
Posts: 55717
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Be very careful about
(size() - 2) / 3
That can go horriblly wrong, producing an array 1 too small, if you start from 61 elements.

I have often seen parallel arrays and inveighed against them. I think this exercise shows how you can get even more confusion by trying to put your parallel arrays into one array/list.

By the way: doesn't the first element in the array tell you how many pairs to expect? That might be another bit of error‑prone design like parallel arrays.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!