• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Index out of bound (can't figure out why)

 
Rancher
Posts: 317
16
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello!
I am trying to solve this problem with linked lists:

https://open.kattis.com/problems/symmetricorder

My code:


My test (so far):


I can't grasp why this is not working? The linked lists are supposed to place one element at the front and one at the back?

I even tried to break but then I have a complete wrong answer:
 
Greenhorn
Posts: 5
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if you use addFirst(); .addLast() you cant read numbers from 0 to n
input n=6
1 2 | 3 4 | 5 6
result
1 2
3 1 2 4
5 3 1 2 4 6

but you can count reversed from n to 0
and swap methods as .addLast(); addFirst(); you get numbers  
result
last(6) first(5):
56
3564
135642

other different solution is here:
(InputReader is not standard class and is not defined in example,  so I use Scanner instead):

 
Marshal
Posts: 79153
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

I don't think either of you has solved the real problem, which is counting indices. I think the only way you can count indices is on paper. Don't use get(i) on a linked list. Don't use add(i, E) on a linked list. Only add or remove things to the middle of a linked list by means of its Iterator. Otherwise you are going to run into problems with poor performance. It would be much quicker to copy the linked list into an array list because that gives you good performance in a for (int i = 0; i < myList.size(); i++) ... loop.The original question says you have a set of Strings preceded by its size. That is slightly strange because the input is presented as a List. There is such a thing as a set which preserves insertion order. The addFirst(E) and addLast(E) methods might give good performance with a linked list, but don't use them because they aren't part of the List interface. What's more, addFirst() and addLast() will leave the short names in the middle of the resultant List. Hint: there are two kinds of iterator to be found from the List interface.

Suggestions:-
  • 1: Don't use a for loop. Use a while loop and an Iterator on your linked list.
  • 2: Work out how to use its Iterator to add elements to the resultant List.
  • 3: Work out how to alternate where you are adding elements.
  •  
    Campbell Ritchie
    Marshal
    Posts: 79153
    377
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You might still end up with the short names in the middle if you follow my suggestion. Nil desperandum; you can get iterators to read backwards, or you can use add(0, E) and add(myList.size(), E), which neither involve iterating the destination list..
     
    D.J. Quavern
    Rancher
    Posts: 317
    16
    IntelliJ IDE Firefox Browser Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    It's really frustrating, I tried to do it with arrays as well but it return null in the middle of the list!



    @Cambell Ritchie: I used i+=2 for the loop read the first name, than the next name. I figured it should go to the third name.
    I did not look into iterators yet!

    @zemiak zet:
    I don't get some stuff
     
    Campbell Ritchie
    Marshal
    Posts: 79153
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Find out what the most frequently used class in the Collections Framework is. I am 99.9% sure it will be this. Create a text file with your words in and read it into a List<String> or a String[] like this:-The two code blocks differ in lines 10 and 15. Never mind the details of the above; look on them as reliable ways to create a List<String> or a String[] without having to write the words repeatedly. For the time being, forget about the numbers to count words. Your difficulty isn't reading words; it is in rearranging them with long words in the middle. You can sort the List and array by word length like this (work out the imports for yourself:-Both those methods use a stable sort, so Anne and Joey won't be swapped. Again, don't go too far into the apparently confusing details.

    Now, you have a reliable source for a List<String> or a String[] sorted by length. Get yourself paper and pencil and draw how you would insert the last element in the List in the middle of your destination List, or similar. Then work out where to put the penultimate element. Remember you are doing arithmeric with the length of the inputList/array.
    If you go through the  documentation for Comparator, you will find this, which may make things easier by sorting your List/array backwards. Remember that an Object[] is filled with nulls until you populate it with real values.
     
    zemiak zet
    Greenhorn
    Posts: 5
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    D.J. Quavern, yes my "other solution" answered mainly to your JUnit test.
    ( I tried to delete it, because it might confuse you, but here's some weird restriction to edit my own posts. )
    Staff note (Liutauras Vilda) :

    Edit restrictions are for a bunch of reasons: https://coderanch.com/t/683892/Request-removing-Editing-restrictions

     
    zemiak zet
    Greenhorn
    Posts: 5
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    D.J. Quavern, You use i for adress where you read word, and where you write word but you increment i twice for loop, so you write word in  places [0, null, 2, null, 3 ... ] and then rewrite words added from end of array.

    So you must use two variables, one for read index, and one for write index. If you use counter for write:



     
    Bartender
    Posts: 5465
    212
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Another possibility (sorry if this has been mentioned before, I may have missed it) is to use an iterator of your LinkedList. Use a counter as well. Remove every element at an odd index and addFirst it to another LinkedList. When done, use addAll.
     
    D.J. Quavern
    Rancher
    Posts: 317
    16
    IntelliJ IDE Firefox Browser Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Weird, I posted a reply but it disappeared.
    I used the fix from zemiak zet, but I definitely need to get back to check iterators.
    Thank you all for your help!
     
    You've gotta fight it! Don't give in! Read this tiny ad:
    a bit of art, as a gift, that will fit in a stocking
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic