Message from @Paragram Slide - AZ
Discord ID: 495328234448224267
```java
import java.lang.*;
```
\`\`\`java
code
\`\`\`
```java
public boolean contains(Object o) {
for (int i = 0; i < size; i++) {
if (o == null && get(i).data == null) {
return true;
}
else if (get(i).data.equals(o)) {
return true;
}
}
return false;
}
```
```java
public ListNode get(int index) throws IndexOutOfBoundsException{
ListNode cur = head;
for (int i = 0; i < index; i++) {
if (i == index) {
return get(i);
}
cur = cur.next;
}
return null;
}
```
pls no bully I haven't taken Java in two years and now I have to relearn everything
get(i).equals
make the first && an ||
but I'm checking to see if they're both null
but the elseif
o could be null
```java
// Returns true if this list contains the specified element o.
// More formally, returns true if and only if this list contains at least one element e
// such that (o==null ? e==null : o.equals(e)).
// Note: you have to handle the case where a list node stores null data element.
```
if o is not null you will move to the else if
and get(i).data could be null
do this
Object.equals(get(i).data, o)
in the else if
handles nulls
says that the method is not applicable for arguments
Objects.equals
ah
I still get a null pointer exception from the if statement
this is what is being tested btw
```java
public static void testContains() { //passed test
init();
System.out.println("----------------testContains()------");
System.out.println(list0.contains(null));
System.out.println(list2.contains("1:Good"));
System.out.println(list2.contains("6:Tony"));
System.out.println(list2.contains("notexist"));
System.out.println(list2.contains(""));
System.out.println(list2.contains(null));
System.out.println(list3.contains("notexist"));
System.out.println(list3.contains(null));
drawLine();
}
```
that is from the tester class, not the same class
break when cur is null
it shouldn't break though
it should theoretically be able to find objects with data set to null
cur.next
cur is never null?
recursively
no it should be null at the last node
```java
// Inserts the specified element at the specified position in this list.
// Shifts the element currently at that position (if any) and any subsequent
// elements to the right (adds one to their indices).
// if(index < 0 or index > this.size), throws IndexOutOfBoundsException.
// E.g, if this list is [dummy]->["A"]->["B"]->["C"] with size = 3.
// add(0,D) will result in [dummy]->["D"]->["A"]->["B"]->["C"].
// Continuing on the previous add() call, add(1,"E") will
// change the existing list to [dummy]->["D"]->["E"]->["A"]->["B"]->["C"].
public void add(int index, Object o) {
ListNode newNode = new ListNode(o);
newNode.next = get(index);
get(index - 1).next = newNode;
this.size++;
}
```
maybe this method is messed up
no wait that one should be okay
```java
//Add the object e to the end of this list.
// it returns true, after e is successfully added.
public boolean add(Object e) {
ListNode newNode = new ListNode(e);
newNode.next = null;
get(this.size - 1).next = newNode;
this.size--;
return true;
}
```
this one might be the one with the problem
why --?
ya lol I just noticed that
that's definitely a mistake
doesn't fix the null pointer exception though