How do I set an element in a list without using LinkedList methods Unicorn Meta Zoo #1: Why another podcast? Announcing the arrival of Valued Associate #679: Cesar Manara Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!How do I check if a list is empty?What is the difference between Python's list methods append and extend?How to randomly select an item from a list?How do you split a list into evenly sized chunks?How do I remove an element from a list by index in Python?Getting the last element of a list in PythonHow to make a flat list out of list of listsHow do I get the number of elements in a list in Python?How do I concatenate two lists in Python?How to clone or copy a list?
Align column where each cell has two decimals with siunitx
Second order approximation of the loss function (Deep learning book, 7.33)
Does Feeblemind produce an ongoing magical effect that can be dispelled?
Can you stand up from being prone using Skirmisher outside of your turn?
Password Generator in batch
How to get even lighting when using flash for group photos near wall?
Suing a Police Officer Instead of the Police Department
Error: Syntax error. Missing ')' for CASE Statement
What is it called when you ride around on your front wheel?
Arriving in Atlanta after US Preclearance in Dublin. Will I go through TSA security in Atlanta to transfer to a connecting flight?
Why is this method for solving linear equations systems using determinants works?
Map material from china not allowed to leave the country
Protagonist's race is hidden - should I reveal it?
What is this word supposed to be?
Is Bran literally the world's memory?
Is this homebrew racial feat, Stonehide, balanced?
Book with legacy programming code on a space ship that the main character hacks to escape
All ASCII characters with a given bit count
My bank got bought out, am I now going to have to start filing tax returns in a different state?
Putting Ant-Man on house arrest
Implementing 3DES algorithm in Java: is my code secure?
Is it acceptable to use working hours to read general interest books?
How to open locks without disable device?
Is there any hidden 'W' sound after 'comment' in : Comment est-elle?
How do I set an element in a list without using LinkedList methods
Unicorn Meta Zoo #1: Why another podcast?
Announcing the arrival of Valued Associate #679: Cesar Manara
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!How do I check if a list is empty?What is the difference between Python's list methods append and extend?How to randomly select an item from a list?How do you split a list into evenly sized chunks?How do I remove an element from a list by index in Python?Getting the last element of a list in PythonHow to make a flat list out of list of listsHow do I get the number of elements in a list in Python?How do I concatenate two lists in Python?How to clone or copy a list?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
For homework, I am working on writing a LinkedList class, that replaces the LinkedList methods. I'm working on the "set" method.
Here's what I have so far for the set() method. It takes in int index
and X item
as parameters. The head of the node is in a variable called first
. (The whole class is genericized.)
Node<X> p = new Node<X>();
if(index < 0 || index > size()-1)
throw new Bonfire();
int count = 0;
while(count != index)
p = p.next;
count++;
if(count == index)
p.item = item;
Node class:
public class Node<T>
T item;
Node<T> next;
When I go to run my code against some test code that I have, it fails the test.
Test Code:
LList<String> b = new LList<String>();
b.add("Hello");
b.add("Bye");
b.set(0, "Bonjour");
assertEquals("Bonjour", b.get(0));
Failed test Reason: org.junit.ComparisonFailure: expected:<[Bonjour]> but was:<[Hello]>
(add(), size(), and get() methods are working correctly.)
So my question is, how do I get this to set the element correctly? From this code, and from why it's failing the test, it looks like it's not setting anything at all. If you need any extra information from me, do not hesitate to ask me. Appreciate the help. Thanks!
java list linked-list
|
show 1 more comment
For homework, I am working on writing a LinkedList class, that replaces the LinkedList methods. I'm working on the "set" method.
Here's what I have so far for the set() method. It takes in int index
and X item
as parameters. The head of the node is in a variable called first
. (The whole class is genericized.)
Node<X> p = new Node<X>();
if(index < 0 || index > size()-1)
throw new Bonfire();
int count = 0;
while(count != index)
p = p.next;
count++;
if(count == index)
p.item = item;
Node class:
public class Node<T>
T item;
Node<T> next;
When I go to run my code against some test code that I have, it fails the test.
Test Code:
LList<String> b = new LList<String>();
b.add("Hello");
b.add("Bye");
b.set(0, "Bonjour");
assertEquals("Bonjour", b.get(0));
Failed test Reason: org.junit.ComparisonFailure: expected:<[Bonjour]> but was:<[Hello]>
(add(), size(), and get() methods are working correctly.)
So my question is, how do I get this to set the element correctly? From this code, and from why it's failing the test, it looks like it's not setting anything at all. If you need any extra information from me, do not hesitate to ask me. Appreciate the help. Thanks!
java list linked-list
Is there a reason why you’re creating a newNode
? Don’t you have the head stored in a field?
– Logan
Mar 9 at 6:16
In my other methods I never used a head.
– Yoshi24517
Mar 9 at 6:21
Well you appear to just be creating a newNode
, which won’t have any links to any otherNode
s. Does your class have any fields?
– Logan
Mar 9 at 6:22
I actually forgot I made a first and last variable for the first and last parts of the list, including in theadd()
method.
– Yoshi24517
Mar 9 at 6:24
Yeah, I assumed that was your issue.
– Logan
Mar 9 at 6:26
|
show 1 more comment
For homework, I am working on writing a LinkedList class, that replaces the LinkedList methods. I'm working on the "set" method.
Here's what I have so far for the set() method. It takes in int index
and X item
as parameters. The head of the node is in a variable called first
. (The whole class is genericized.)
Node<X> p = new Node<X>();
if(index < 0 || index > size()-1)
throw new Bonfire();
int count = 0;
while(count != index)
p = p.next;
count++;
if(count == index)
p.item = item;
Node class:
public class Node<T>
T item;
Node<T> next;
When I go to run my code against some test code that I have, it fails the test.
Test Code:
LList<String> b = new LList<String>();
b.add("Hello");
b.add("Bye");
b.set(0, "Bonjour");
assertEquals("Bonjour", b.get(0));
Failed test Reason: org.junit.ComparisonFailure: expected:<[Bonjour]> but was:<[Hello]>
(add(), size(), and get() methods are working correctly.)
So my question is, how do I get this to set the element correctly? From this code, and from why it's failing the test, it looks like it's not setting anything at all. If you need any extra information from me, do not hesitate to ask me. Appreciate the help. Thanks!
java list linked-list
For homework, I am working on writing a LinkedList class, that replaces the LinkedList methods. I'm working on the "set" method.
Here's what I have so far for the set() method. It takes in int index
and X item
as parameters. The head of the node is in a variable called first
. (The whole class is genericized.)
Node<X> p = new Node<X>();
if(index < 0 || index > size()-1)
throw new Bonfire();
int count = 0;
while(count != index)
p = p.next;
count++;
if(count == index)
p.item = item;
Node class:
public class Node<T>
T item;
Node<T> next;
When I go to run my code against some test code that I have, it fails the test.
Test Code:
LList<String> b = new LList<String>();
b.add("Hello");
b.add("Bye");
b.set(0, "Bonjour");
assertEquals("Bonjour", b.get(0));
Failed test Reason: org.junit.ComparisonFailure: expected:<[Bonjour]> but was:<[Hello]>
(add(), size(), and get() methods are working correctly.)
So my question is, how do I get this to set the element correctly? From this code, and from why it's failing the test, it looks like it's not setting anything at all. If you need any extra information from me, do not hesitate to ask me. Appreciate the help. Thanks!
java list linked-list
java list linked-list
edited Mar 9 at 6:28
Yoshi24517
asked Mar 9 at 6:13
Yoshi24517Yoshi24517
1408
1408
Is there a reason why you’re creating a newNode
? Don’t you have the head stored in a field?
– Logan
Mar 9 at 6:16
In my other methods I never used a head.
– Yoshi24517
Mar 9 at 6:21
Well you appear to just be creating a newNode
, which won’t have any links to any otherNode
s. Does your class have any fields?
– Logan
Mar 9 at 6:22
I actually forgot I made a first and last variable for the first and last parts of the list, including in theadd()
method.
– Yoshi24517
Mar 9 at 6:24
Yeah, I assumed that was your issue.
– Logan
Mar 9 at 6:26
|
show 1 more comment
Is there a reason why you’re creating a newNode
? Don’t you have the head stored in a field?
– Logan
Mar 9 at 6:16
In my other methods I never used a head.
– Yoshi24517
Mar 9 at 6:21
Well you appear to just be creating a newNode
, which won’t have any links to any otherNode
s. Does your class have any fields?
– Logan
Mar 9 at 6:22
I actually forgot I made a first and last variable for the first and last parts of the list, including in theadd()
method.
– Yoshi24517
Mar 9 at 6:24
Yeah, I assumed that was your issue.
– Logan
Mar 9 at 6:26
Is there a reason why you’re creating a new
Node
? Don’t you have the head stored in a field?– Logan
Mar 9 at 6:16
Is there a reason why you’re creating a new
Node
? Don’t you have the head stored in a field?– Logan
Mar 9 at 6:16
In my other methods I never used a head.
– Yoshi24517
Mar 9 at 6:21
In my other methods I never used a head.
– Yoshi24517
Mar 9 at 6:21
Well you appear to just be creating a new
Node
, which won’t have any links to any other Node
s. Does your class have any fields?– Logan
Mar 9 at 6:22
Well you appear to just be creating a new
Node
, which won’t have any links to any other Node
s. Does your class have any fields?– Logan
Mar 9 at 6:22
I actually forgot I made a first and last variable for the first and last parts of the list, including in the
add()
method.– Yoshi24517
Mar 9 at 6:24
I actually forgot I made a first and last variable for the first and last parts of the list, including in the
add()
method.– Yoshi24517
Mar 9 at 6:24
Yeah, I assumed that was your issue.
– Logan
Mar 9 at 6:26
Yeah, I assumed that was your issue.
– Logan
Mar 9 at 6:26
|
show 1 more comment
1 Answer
1
active
oldest
votes
All I needed to do was change Node<X> p
to make it Node<X> p = first;
.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55074574%2fhow-do-i-set-an-element-in-a-list-without-using-linkedlist-methods%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
All I needed to do was change Node<X> p
to make it Node<X> p = first;
.
add a comment |
All I needed to do was change Node<X> p
to make it Node<X> p = first;
.
add a comment |
All I needed to do was change Node<X> p
to make it Node<X> p = first;
.
All I needed to do was change Node<X> p
to make it Node<X> p = first;
.
answered Mar 9 at 6:25
Yoshi24517Yoshi24517
1408
1408
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55074574%2fhow-do-i-set-an-element-in-a-list-without-using-linkedlist-methods%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Is there a reason why you’re creating a new
Node
? Don’t you have the head stored in a field?– Logan
Mar 9 at 6:16
In my other methods I never used a head.
– Yoshi24517
Mar 9 at 6:21
Well you appear to just be creating a new
Node
, which won’t have any links to any otherNode
s. Does your class have any fields?– Logan
Mar 9 at 6:22
I actually forgot I made a first and last variable for the first and last parts of the list, including in the
add()
method.– Yoshi24517
Mar 9 at 6:24
Yeah, I assumed that was your issue.
– Logan
Mar 9 at 6:26