https://kotlinlang.org logo
#getting-started
Title
# getting-started
d

Dan O'Brien

09/09/2020, 1:00 AM
is there something I’m completely missing about using a while loop within a for loop? The while loop is looping over a list of JSwing nodes, the for loop is iterating over a list of items but the for loops only runs a single time then just does not loop, I don’t get it. I originally tried a foreach, and now I’m just adding print statements to try to understand what I’m doing wrong. I know this code is not the cleanest/most correct lol. Learning Kotlin as I go
and to clarify the
size
prints as 29 and the
idx
var never increments above 0
m

Michael de Kaste

09/09/2020, 2:11 PM
hard to debug someone else's code, but idx should increment. So what happens? You just go past the try? Do you catch an error?
also, debugging by printing is generally not smart if you just have access to the debugger
d

Dan O'Brien

09/09/2020, 3:29 PM
thanks @Michael de Kaste! I ended up running it through the debugger and still not sure of what was causing issue in first place but reworked some code and it loops as expected 🤷‍♂️
n

nanodeath

09/09/2020, 3:33 PM
aside: I'd convert that Enumeration into anything else first...using
Collections.list()
maybe. or maybe there's a asSequence adapter you can use/write
d

Dan O'Brien

09/09/2020, 3:35 PM
appreciate it, I’ll look to do that. Is there specific performance or readability reasons for the change?
n

nanodeath

09/09/2020, 3:37 PM
Enumerations are just very...Java 1.0. doesn't look like you can even use them directly in for-each, right? manually iterating things can be a source of errors
if it was a sequence or list you wouldn't need that
parent.userObject is FlagNodeParent
check either; you could use
.filterIsInstance<FlagNodeParent>
d

Dan O'Brien

09/09/2020, 3:38 PM
lol I’m not entirely sure myself, I found a project to use Kotlin on to also learn some about the JVM. Loving Kotlin so far, but I’m very much in the early stages of my understanding
n

nanodeath

09/09/2020, 3:39 PM
if you pasted another version with all the debug statements and commenting code removed, and enabled Kotlin syntax highlighting...that would make it easier for others to read.
you've tried attaching a debugger, too? often that's the best way to see what's going on
oh sorry, yeah you tried that
eh, I guess it's working now?
d

Dan O'Brien

09/09/2020, 3:42 PM
for this problem 😄 onto other ones of my own making with lack of understanding. Thanks again for the help and pointers!
n

nanodeath

09/09/2020, 3:47 PM
image.png
3 Views