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;








0















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!










share|improve this question
























  • 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 other Nodes. 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

















0















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!










share|improve this question
























  • 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 other Nodes. 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













0












0








0








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!










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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 other Nodes. 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

















  • 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 other Nodes. 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
















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 Nodes. 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 Nodes. 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












1 Answer
1






active

oldest

votes


















1














All I needed to do was change Node<X> p to make it Node<X> p = first;.






share|improve this answer























    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
    );



    );













    draft saved

    draft discarded


















    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









    1














    All I needed to do was change Node<X> p to make it Node<X> p = first;.






    share|improve this answer



























      1














      All I needed to do was change Node<X> p to make it Node<X> p = first;.






      share|improve this answer

























        1












        1








        1







        All I needed to do was change Node<X> p to make it Node<X> p = first;.






        share|improve this answer













        All I needed to do was change Node<X> p to make it Node<X> p = first;.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 9 at 6:25









        Yoshi24517Yoshi24517

        1408




        1408





























            draft saved

            draft discarded
















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Save data to MySQL database using ExtJS and PHP [closed]2019 Community Moderator ElectionHow can I prevent SQL injection in PHP?Which MySQL data type to use for storing boolean valuesPHP: Delete an element from an arrayHow do I connect to a MySQL Database in Python?Should I use the datetime or timestamp data type in MySQL?How to get a list of MySQL user accountsHow Do You Parse and Process HTML/XML in PHP?Reference — What does this symbol mean in PHP?How does PHP 'foreach' actually work?Why shouldn't I use mysql_* functions in PHP?

            Compiling GNU Global with universal-ctags support Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Tags for Emacs: Relationship between etags, ebrowse, cscope, GNU Global and exuberant ctagsVim and Ctags tips and trickscscope or ctags why choose one over the other?scons and ctagsctags cannot open option file “.ctags”Adding tag scopes in universal-ctagsShould I use Universal-ctags?Universal ctags on WindowsHow do I install GNU Global with universal ctags support using Homebrew?Universal ctags with emacsHow to highlight ctags generated by Universal Ctags in Vim?

            Add ONERROR event to image from jsp tldHow to add an image to a JPanel?Saving image from PHP URLHTML img scalingCheck if an image is loaded (no errors) with jQueryHow to force an <img> to take up width, even if the image is not loadedHow do I populate hidden form field with a value set in Spring ControllerStyling Raw elements Generated from JSP tagds with Jquery MobileLimit resizing of images with explicitly set width and height attributeserror TLD use in a jsp fileJsp tld files cannot be resolved