Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Kuule hain nussivan
Nov 27, 2008

samcarsten posted:

but if i change it to an if;else, the it doesn't iterate. Where should I put the return false?
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.

Adbot
ADBOT LOVES YOU

samcarsten
Sep 13, 2022

by vyelkin

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.

Kuule hain nussivan
Nov 27, 2008

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:
A loop
	An if clause
		A true return
	End of if clause
End of loop
A false return
The code will return true when the if clause succeeds. At that point, the method will cease to be run and return true.
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.

samcarsten
Sep 13, 2022

by vyelkin
uh oh. I need to manually program a "get" command. How the gently caress do I do that?

Kuule hain nussivan
Nov 27, 2008

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.

samcarsten
Sep 13, 2022

by vyelkin

Kuule hain nussivan posted:

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.

according to my IDE, that is incorrect. list[0] only applies to arrays, not lists.

Kuule hain nussivan
Nov 27, 2008

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/

samcarsten
Sep 13, 2022

by vyelkin

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.

hooah
Feb 6, 2006
WTF?
Just use your for loop again. Go until you get to the requested index or throw an exception.

samcarsten
Sep 13, 2022

by vyelkin

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?

Kuule hain nussivan
Nov 27, 2008

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?

samcarsten
Sep 13, 2022

by vyelkin

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

Kuule hain nussivan
Nov 27, 2008

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.

https://drive.google.com/file/d/14LF1ayGb9xbno4EggglcE8qVVoLKBvh7/view?usp=share_link

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.

samcarsten
Sep 13, 2022

by vyelkin

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?

samcarsten
Sep 13, 2022

by vyelkin
ok, so I have some code, but also an error.

code:
	  @Override /** Return the element at the specified index */
	  public E get(int index) {
		  Iterator<E> it = this.iterator();
		  for (int i = 0; i < this.size(); i++) {
			  it = it.next();
			  if (i == index) {
				  return it;
			  }
		  }
	    return null;
	  }
The error is: Type mismatch: cannot convert from E to Iterator<E>

Kuule hain nussivan
Nov 27, 2008

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:
Check that given index is positive
Check that given index is not outside the bounds of the collection
Get iterator
Traverse iterator until it has returned the object at the given index
Return object
The traversal should be done (roughly) like so:

code:
var object;
for i = 0; i <= index; i++ {
	object = iterator.next()
}
Let me know if you need any extra help.

Edit:

samcarsten posted:

code:
	  @Override /** Return the element at the specified index */
	  public E get(int index) {
		  Iterator<E> it = this.iterator();
		  for (int i = 0; i < this.size(); i++) {
			  it = it.next();
			  if (i == index) {
				  return it;
			  }
		  }
	    return null;
	  }
The error is: Type mismatch: cannot convert from E to Iterator<E>

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.

samcarsten
Sep 13, 2022

by vyelkin
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?

Kuule hain nussivan
Nov 27, 2008

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.

samcarsten
Sep 13, 2022

by vyelkin
ok, I think I've got it.

code:
	  @Override /** Replace the element at the specified position 
	   *  in this list with the specified element. */
	  public E set(int index, E e) {
		  Iterator<E> it = this.iterator();
		  for (int i = 0; i < this.size; i++) {
			  E entry = it.next();
			  if (index == i) {
				 this.remove(entry);
				 this.add(i, e);
			  }
		  }
	    return null;
	  }
Now, the next thing I need to do is program the "remove" command for Iterator.

samcarsten
Sep 13, 2022

by vyelkin
couldn't figure out the iterator, working on next bit.

code:
  @Override
  public default boolean addAll(Collection<? extends E> c) {
	  Iterator<?> num1 = c.iterator();
	  for (Object x : c) {
		  Object thing = num1.next();
		  this.add(thing);
	  }
    return true;
  }
this is throwing up the error: The method add(E) in the type MyList<E> is not applicable for the arguments (Object)

Kilson
Jan 16, 2003

I EAT LITTLE CHILDREN FOR BREAKFAST !!11!!1!!!!111!

samcarsten posted:

couldn't figure out the iterator, working on next bit.

code:
  @Override
  public default boolean addAll(Collection<? extends E> c) {
	  Iterator<?> num1 = c.iterator();
	  for (Object x : c) {
		  Object thing = num1.next();
		  this.add(thing);
	  }
    return true;
  }
this is throwing up the error: The method add(E) in the type MyList<E> is not applicable for the arguments (Object)

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

samcarsten
Sep 13, 2022

by vyelkin
ok, figured most of this out by now. Now, how do I program the "remove" command in this code section?

code:
 @Override /** Override iterator() defined in Iterable */
	  public java.util.Iterator<E> iterator() {
	    return new LinkedListIterator();
	  }
	  
	  private class LinkedListIterator 
	      implements java.util.Iterator<E> {
	    private Node<E> current = head; // Current index 
	    
	    @Override
	    public boolean hasNext() {
	      return (current != null);
	    }

	    @Override
	    public E next() {
	      E e = current.element;
	      current = current.next;
	      return e;
	    }

	    @Override
	    public void remove() {

	    }

necrotic
Aug 2, 2005
I owe my brother big time for this!

samcarsten posted:

ok, figured most of this out by now. Now, how do I program the "remove" command in this code section?

code:
 @Override /** Override iterator() defined in Iterable */
	  public java.util.Iterator<E> iterator() {
	    return new LinkedListIterator();
	  }
	  
	  private class LinkedListIterator 
	      implements java.util.Iterator<E> {
	    private Node<E> current = head; // Current index 
	    
	    @Override
	    public boolean hasNext() {
	      return (current != null);
	    }

	    @Override
	    public E next() {
	      E e = current.element;
	      current = current.next;
	      return e;
	    }

	    @Override
	    public void remove() {

	    }

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?

samcarsten
Sep 13, 2022

by vyelkin

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.

necrotic
Aug 2, 2005
I owe my brother big time for this!

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?

samcarsten
Sep 13, 2022

by vyelkin

necrotic posted:

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?

shift everything left?

necrotic
Aug 2, 2005
I owe my brother big time for this!

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.

samcarsten
Sep 13, 2022

by vyelkin

necrotic posted:

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.

make node 1 point at node 3? How do i do that?

necrotic
Aug 2, 2005
I owe my brother big time for this!

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.

samcarsten
Sep 13, 2022

by vyelkin
yeah, that's not very helpful.

hooah
Feb 6, 2006
WTF?

samcarsten posted:

yeah, that's not very helpful.

Maybe try talking to your professor or TA.

Tesseraction
Apr 5, 2009

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.

samcarsten
Sep 13, 2022

by vyelkin

hooah posted:

Maybe try talking to your professor or TA.

lol, i go to a community college. These people barely check their email.

hooah
Feb 6, 2006
WTF?

samcarsten posted:

lol, i go to a community college. These people barely check their email.

Well that sucks. No office hours?

samcarsten
Sep 13, 2022

by vyelkin

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:
@Override
  public default Object[] toArray() {
    // Left as an exercise
    return null;
  }

  @Override
  public default <T> T[] toArray(T[] array) {
    // Left as an exercise
    return null;
  }
I'm not sure what the difference between the two is, though.

Jabor
Jul 16, 2010

#1 Loser at SpaceChem
You can look at the documentation of those methods to see what they're supposed to do.

VegasGoat
Nov 9, 2011

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.

samcarsten
Sep 13, 2022

by vyelkin
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:
	  @Override /** Return true if this list contains the element e */
	  public boolean contains(Object e) {
		  for (int i = 0; i < this.size(); i++) {
	      	if (e == this.get(i)) {
	        	return true;
	      	}
	        }
		return false;
	  }
For a list of strings I am given, this is not returning correctly. If I change == to the equals() command, it doesn't work either. It consistently returns false. What am I doing wrong?

hooah
Feb 6, 2006
WTF?
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.

Adbot
ADBOT LOVES YOU

Clockwerk
Apr 6, 2005


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"

code:
	  @Override /** Return true if this list contains the element e */
	  public boolean contains(Object e) {
		  for (int i = 0; i < this.size(); i++) {
	      	if (e == this.get(i)) {
	        	return true;
	      	}
	        }
		return false;
	  }
For a list of strings I am given, this is not returning correctly. If I change == to the equals() command, it doesn't work either. It consistently returns false. What am I doing wrong?

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

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply