|
samcarsten posted:but if i change it to an if;else, the it doesn't iterate. Where should I put the return false?
|
# ? Nov 1, 2022 12:27 |
|
|
# ? Jun 12, 2024 04:48 |
|
Kuule hain nussivan posted:So basically, you have a case where it loops through the whole list, but doesn't find a match. So the point where you want to return false (no match) is once you've looped but the list without having returned true. can you do a for;else? Otherwise, if it put it outside the loop, it's always going to return false.
|
# ? Nov 1, 2022 12:37 |
|
samcarsten posted:can you do a for;else? Otherwise, if it put it outside the loop, it's always going to return false. Not necessarily. A method can only return once. Let's say you have the following: code:
And if the if clause doesn't succeed at any point, the loop will finish running and the method will continue with the code after it, in this case returning false.
|
# ? Nov 1, 2022 12:41 |
|
uh oh. I need to manually program a "get" command. How the gently caress do I do that?
|
# ? Nov 1, 2022 12:47 |
|
samcarsten posted:uh oh. I need to manually program a "get" command. How the gently caress do I do that? Is this for the Contains method we already talked about, or another part of the assignment? Either way, it's relatively straightforward. You want to know that an alternative syntax for list.get(0) is list[0]. Once you know that, it's really only a matter of checking whether the given index is 0 or larger and whether the list contains enough objects to return the given index.
|
# ? Nov 1, 2022 12:56 |
|
Kuule hain nussivan posted:Is this for the Contains method we already talked about, or another part of the assignment? according to my IDE, that is incorrect. list[0] only applies to arrays, not lists.
|
# ? Nov 1, 2022 13:07 |
|
samcarsten posted:according to my IDE, that is incorrect. list[0] only applies to arrays, not lists. You are absolutely correct, I misremembered. In that case, I think they want you to use an iterator. This should give you an idea: https://www.geeksforgeeks.org/listiterator-in-java/
|
# ? Nov 1, 2022 13:20 |
|
Kuule hain nussivan posted:You are absolutely correct, I misremembered. In that case, I think they want you to use an iterator. This should give you an idea: https://www.geeksforgeeks.org/listiterator-in-java/ hmm, according to my IDE this is undefined for my type. remember, I can't use inbuilt stuff, i need to manually program them.
|
# ? Nov 1, 2022 13:24 |
|
Just use your for loop again. Go until you get to the requested index or throw an exception.
|
# ? Nov 1, 2022 13:28 |
|
hooah posted:Just use your for loop again. Go until you get to the requested index or throw an exception. uh, how do i get it to choose the entry if i can't use get?
|
# ? Nov 1, 2022 13:31 |
|
samcarsten posted:hmm, according to my IDE this is undefined for my type. remember, I can't use inbuilt stuff, i need to manually program them. I went back on the messages and I think I completely misunderstood the assignment. You've implemented your own data storage class, right? How are you storing information in it?
|
# ? Nov 1, 2022 13:31 |
|
Kuule hain nussivan posted:I went back on the messages and I think I completely misunderstood the assignment. You've implemented your own data storage class, right? How are you storing information in it? I was given some code and told to write the remaining methods not already implemented. I'll throw the code up on google drive, its too long to post. https://drive.google.com/file/d/14LF1ayGb9xbno4EggglcE8qVVoLKBvh7/view?usp=share_link
|
# ? Nov 1, 2022 13:34 |
|
samcarsten posted:I was given some code and told to write the remaining methods not already implemented. I'll throw the code up on google drive, its too long to post. Okay, so the backing class is a Collection. In that case, as far as I know, there's no other way of getting the info you want out other than an iterator or converting it to an Array or ArrayList and using the [] operator. Since the assignment code itself is using Collection as a backing class, I'd say they should be fine with you using the tools associated with it.
|
# ? Nov 1, 2022 13:40 |
|
Kuule hain nussivan posted:Okay, so the backing class is a Collection. In that case, as far as I know, there's no other way of getting the info you want out other than an iterator or converting it to an Array or ArrayList and using the [] operator. Since the assignment code itself is using Collection as a backing class, I'd say they should be fine with you using the tools associated with it. given i have to manually program "toArray" as well, i'll just use an iterator. Now, how do i get the iterator to choose an entry without "get"? I can make a number go up in a for loop, but how do i associate it with an entry?
|
# ? Nov 1, 2022 13:43 |
|
ok, so I have some code, but also an error. code:
|
# ? Nov 1, 2022 13:49 |
|
samcarsten posted:given i have to manually program "toArray" as well, i'll just use an iterator. Now, how do i get the iterator to choose an entry without "get"? I can make a number go up in a for loop, but how do i associate it with an entry? The iterator for the collection has two methods that interest you; hasNext(), which tells you if the iterator has any objects left to traverse, and next(), which returns the next objects and "moves" the iterator forward. So the basic pseudocode for your get() method should be something like below. code:
code:
Edit: samcarsten posted:
The trouble here is that you're trying to assign the object returned by the iterator into the slot assigned for the iterator when you do it = it.next(). You should create a separate variable to hold the object returned.
|
# ? Nov 1, 2022 13:50 |
|
ok, next issue. I need to manually program the "set" command. I can get it to itertate to the correct entry, but how do I replace one entry with another?
|
# ? Nov 1, 2022 14:03 |
|
samcarsten posted:ok, next issue. I need to manually program the "set" command. I can get it to itertate to the correct entry, but how do I replace one entry with another? I think you're going to need someone with more experience in Java. The only way I can think of off the top of my head is to convert the collection into an array, do what you must and then convert it back, which seems like something you shouldn't do.
|
# ? Nov 1, 2022 14:36 |
|
ok, I think I've got it.code:
|
# ? Nov 1, 2022 15:41 |
|
couldn't figure out the iterator, working on next bit.code:
|
# ? Nov 1, 2022 16:04 |
|
samcarsten posted:couldn't figure out the iterator, working on next bit. You don't need to use an iterator for addAll. You're already looping through the elements you want to add (and assigning each one to x), so just add x each time. The type problem you're having is because you're giving x the type Object. It might need the actual type there for it to not complain
|
# ? Nov 1, 2022 21:30 |
|
ok, figured most of this out by now. Now, how do I program the "remove" command in this code section?code:
|
# ? Nov 2, 2022 00:56 |
|
samcarsten posted:ok, figured most of this out by now. Now, how do I program the "remove" command in this code section? If the collection is a linked list with the next element on each node, what would be needed to “remove” the current node from the list?
|
# ? Nov 2, 2022 01:32 |
|
necrotic posted:If the collection is a linked list with the next element on each node, what would be needed to “remove” the current node from the list? no idea. there's not a "delete" command.
|
# ? Nov 2, 2022 01:42 |
|
samcarsten posted:no idea. there's not a "delete" command. Consider the shape of how the “list” exists. If you have three nodes: - node 1 with next pointing at node 2 - node 2 with next pointing at node 3 - node 3 with next null How do we change the “list” when we remove node 2?
|
# ? Nov 2, 2022 01:47 |
|
necrotic posted:Consider the shape of how the “list” exists. If you have three nodes: shift everything left?
|
# ? Nov 2, 2022 13:58 |
|
samcarsten posted:shift everything left? If there are nodes after the third (instead of the end of the list), do they need to be shifted explicitly? Take my bullet list, what actually changes when node 2 is “deleted”? That is what you need to do.
|
# ? Nov 2, 2022 15:16 |
|
necrotic posted:If there are nodes after the third (instead of the end of the list), do they need to be shifted explicitly? make node 1 point at node 3? How do i do that?
|
# ? Nov 2, 2022 19:53 |
|
samcarsten posted:make node 1 point at node 3? How do i do that? You might need to keep track of the previous node in your iterator to accomplish this. I’m not going to give anything beyond hints because I’m not here to do your homework for you, but hopefully help you understand the problem solving process.
|
# ? Nov 2, 2022 20:06 |
|
yeah, that's not very helpful.
|
# ? Nov 3, 2022 10:02 |
|
samcarsten posted:yeah, that's not very helpful. Maybe try talking to your professor or TA.
|
# ? Nov 3, 2022 11:19 |
|
Think about what a LinkedList is as a data structure, and think about how an iterator might need to take more things into account for it to iterate.
|
# ? Nov 3, 2022 11:32 |
|
hooah posted:Maybe try talking to your professor or TA. lol, i go to a community college. These people barely check their email.
|
# ? Nov 3, 2022 13:27 |
|
samcarsten posted:lol, i go to a community college. These people barely check their email. Well that sucks. No office hours?
|
# ? Nov 3, 2022 14:32 |
|
hooah posted:Well that sucks. No office hours? don't have a laptop to bring to show the issue. Anyway, the last two things I need to do besides the iterator are the following methods: code:
|
# ? Nov 3, 2022 14:39 |
|
You can look at the documentation of those methods to see what they're supposed to do.
|
# ? Nov 3, 2022 15:03 |
|
samcarsten posted:I'm not sure what the difference between the two is, though. Might help if you read the description of those methods on the Collection interface you’re implementing for this assignment. It explains how they should work. https://docs.oracle.com/javase/8/docs/api Choose Collection from the list on the left to see the info for that interface and you’ll see those same toArray methods. You can literally use “T” as a type in the second one if you weren’t aware.
|
# ? Nov 3, 2022 15:09 |
|
Okay, reviewed my assignment and apparently, despite the incomplete code I've been given, I'm only supposed to do 5 of the methods. Now I'm trying to review my code and fix it. Let's start with method "contains"code:
|
# ? Nov 3, 2022 15:49 |
|
First of all, either start exploring the debugger, or print out each thing in the collection as you're iterating over it. Possibly also print the type of that item.
|
# ? Nov 3, 2022 15:52 |
|
|
# ? Jun 12, 2024 04:48 |
|
samcarsten posted:Okay, reviewed my assignment and apparently, despite the incomplete code I've been given, I'm only supposed to do 5 of the methods. Now I'm trying to review my code and fix it. Let's start with method "contains" I would expect equals() to work here, but it can depend on what the object actually is and assuming its implementation of equals() is sane. There is potential here for a malevolent curveball thrown by someone who hates their students, but it’s probably not that. I would be more suspicious of the setup prior to this test having a flaw. As the other poster said, it’s a good opportunity to get familiar with your debugger and how to step through it and monitor state. You can even set a breakpoint on the equals method of the class being tested and see what it’s evaluating. Update: just realized the issue may be with the get(i) call. Is it actually retrieving by an array index or trying to find by doing its own equals() and looking for a match? Clockwerk fucked around with this message at 12:49 on Nov 4, 2022 |
# ? Nov 4, 2022 02:50 |