Using nested for loop to add 2 Linked Lists with java.util.ListIterator;










-2















I am trying to iterate through two Linked lists using a for loop and the listIterator method.



I am trying to accomplish the method below which adds two polynomials using a linked list



public Polynomial add(Polynomial p)



POSTCONDITION: this object and p are not changed
return a polynomial that is the sum of p and this polynomial



Here is my method



public Polynomial add( Polynomial p )

// use the copy constructor
Polynomial answer = new Polynomial( this );
// answer.termList.addAll(p.termList);
// use addAll()
//answer=this.termList.addAll(p.termList);
//ListIterator<Term> itr = answer.termList.listIterator();
//ListIterator<Term> itr = answer.termList.listIterator();
//ListIterator<Term> itr2 = p.termList.listIterator();
for ( ListIterator<Term> itr = answer.termList.listIterator(); itr.hasNext(); )

Term term = itr.next();

for ( ListIterator<Term> itr2 = p.termList.listIterator(); itr2.hasNext(); )

Term term2 = itr2.next();
if ( term.exponent == term2.exponent )

answer.itr.coefficient = answer.itr.coefficient + p.itr2.coefficient;


/**while ( itr.hasNext() )

Term term = itr.next();
**/
return answer;





This line for (ListIterator<Term> itr=answer.termList.listIterator();itr.hasNext();) and for(ListIterator<Term> itr2 = p.termList.listIterator(); itr2.hasNext();) have errors.



I keep getting an error that says itr and itr2 cannot be resolved or is not a field.



Here is part of the code that defines Polynomial there are a lot of methods which I didn't include since they are too long.



public class Polynomial implements Cloneable

// this is a nested static class, it defines a type
// all the instance varaibles can be access directly inside Polynomial
// class even though they have

// private modifier
private static class Term

private int exponent;
private double coefficient;

public Term( int exp, double coeff )

coefficient = coeff;
exponent = exp;



// instance variables of Polynomial
// first is the term of the Polynomial with the highest degree
private LinkedList<Term> termList;

/**
* Postcondition: Creates a polynomial which is 0.
**/
public Polynomial()

termList = new LinkedList<Term>();
termList.add( new Term( 0, 0 ) );


/**
* Postcondition: Creates a polynomial which has a single term
* a0*x^0
*
* @param a0 The value to be set as the coefficient of the
* constant (x^0) t erm.
**/
public Polynomial( double a0 )

termList = new LinkedList<Term>();
termList.add( new Term( 0, a0 ) );











share|improve this question
























  • Please post the code that defines Polynomial

    – mjuarez
    Nov 13 '18 at 5:58











  • Please indicate which line(s) has the errors

    – Scary Wombat
    Nov 13 '18 at 5:59






  • 1





    come-on, please make some effort to format your code

    – Scary Wombat
    Nov 13 '18 at 6:04











  • the termList member is declared private, isn’t this an issue in the add method? And please remove all code that has been commented out.

    – Joakim Danielson
    Nov 13 '18 at 6:08












  • @ScaryWombat what do you need?

    – Preston
    Nov 13 '18 at 6:09















-2















I am trying to iterate through two Linked lists using a for loop and the listIterator method.



I am trying to accomplish the method below which adds two polynomials using a linked list



public Polynomial add(Polynomial p)



POSTCONDITION: this object and p are not changed
return a polynomial that is the sum of p and this polynomial



Here is my method



public Polynomial add( Polynomial p )

// use the copy constructor
Polynomial answer = new Polynomial( this );
// answer.termList.addAll(p.termList);
// use addAll()
//answer=this.termList.addAll(p.termList);
//ListIterator<Term> itr = answer.termList.listIterator();
//ListIterator<Term> itr = answer.termList.listIterator();
//ListIterator<Term> itr2 = p.termList.listIterator();
for ( ListIterator<Term> itr = answer.termList.listIterator(); itr.hasNext(); )

Term term = itr.next();

for ( ListIterator<Term> itr2 = p.termList.listIterator(); itr2.hasNext(); )

Term term2 = itr2.next();
if ( term.exponent == term2.exponent )

answer.itr.coefficient = answer.itr.coefficient + p.itr2.coefficient;


/**while ( itr.hasNext() )

Term term = itr.next();
**/
return answer;





This line for (ListIterator<Term> itr=answer.termList.listIterator();itr.hasNext();) and for(ListIterator<Term> itr2 = p.termList.listIterator(); itr2.hasNext();) have errors.



I keep getting an error that says itr and itr2 cannot be resolved or is not a field.



Here is part of the code that defines Polynomial there are a lot of methods which I didn't include since they are too long.



public class Polynomial implements Cloneable

// this is a nested static class, it defines a type
// all the instance varaibles can be access directly inside Polynomial
// class even though they have

// private modifier
private static class Term

private int exponent;
private double coefficient;

public Term( int exp, double coeff )

coefficient = coeff;
exponent = exp;



// instance variables of Polynomial
// first is the term of the Polynomial with the highest degree
private LinkedList<Term> termList;

/**
* Postcondition: Creates a polynomial which is 0.
**/
public Polynomial()

termList = new LinkedList<Term>();
termList.add( new Term( 0, 0 ) );


/**
* Postcondition: Creates a polynomial which has a single term
* a0*x^0
*
* @param a0 The value to be set as the coefficient of the
* constant (x^0) t erm.
**/
public Polynomial( double a0 )

termList = new LinkedList<Term>();
termList.add( new Term( 0, a0 ) );











share|improve this question
























  • Please post the code that defines Polynomial

    – mjuarez
    Nov 13 '18 at 5:58











  • Please indicate which line(s) has the errors

    – Scary Wombat
    Nov 13 '18 at 5:59






  • 1





    come-on, please make some effort to format your code

    – Scary Wombat
    Nov 13 '18 at 6:04











  • the termList member is declared private, isn’t this an issue in the add method? And please remove all code that has been commented out.

    – Joakim Danielson
    Nov 13 '18 at 6:08












  • @ScaryWombat what do you need?

    – Preston
    Nov 13 '18 at 6:09













-2












-2








-2








I am trying to iterate through two Linked lists using a for loop and the listIterator method.



I am trying to accomplish the method below which adds two polynomials using a linked list



public Polynomial add(Polynomial p)



POSTCONDITION: this object and p are not changed
return a polynomial that is the sum of p and this polynomial



Here is my method



public Polynomial add( Polynomial p )

// use the copy constructor
Polynomial answer = new Polynomial( this );
// answer.termList.addAll(p.termList);
// use addAll()
//answer=this.termList.addAll(p.termList);
//ListIterator<Term> itr = answer.termList.listIterator();
//ListIterator<Term> itr = answer.termList.listIterator();
//ListIterator<Term> itr2 = p.termList.listIterator();
for ( ListIterator<Term> itr = answer.termList.listIterator(); itr.hasNext(); )

Term term = itr.next();

for ( ListIterator<Term> itr2 = p.termList.listIterator(); itr2.hasNext(); )

Term term2 = itr2.next();
if ( term.exponent == term2.exponent )

answer.itr.coefficient = answer.itr.coefficient + p.itr2.coefficient;


/**while ( itr.hasNext() )

Term term = itr.next();
**/
return answer;





This line for (ListIterator<Term> itr=answer.termList.listIterator();itr.hasNext();) and for(ListIterator<Term> itr2 = p.termList.listIterator(); itr2.hasNext();) have errors.



I keep getting an error that says itr and itr2 cannot be resolved or is not a field.



Here is part of the code that defines Polynomial there are a lot of methods which I didn't include since they are too long.



public class Polynomial implements Cloneable

// this is a nested static class, it defines a type
// all the instance varaibles can be access directly inside Polynomial
// class even though they have

// private modifier
private static class Term

private int exponent;
private double coefficient;

public Term( int exp, double coeff )

coefficient = coeff;
exponent = exp;



// instance variables of Polynomial
// first is the term of the Polynomial with the highest degree
private LinkedList<Term> termList;

/**
* Postcondition: Creates a polynomial which is 0.
**/
public Polynomial()

termList = new LinkedList<Term>();
termList.add( new Term( 0, 0 ) );


/**
* Postcondition: Creates a polynomial which has a single term
* a0*x^0
*
* @param a0 The value to be set as the coefficient of the
* constant (x^0) t erm.
**/
public Polynomial( double a0 )

termList = new LinkedList<Term>();
termList.add( new Term( 0, a0 ) );











share|improve this question
















I am trying to iterate through two Linked lists using a for loop and the listIterator method.



I am trying to accomplish the method below which adds two polynomials using a linked list



public Polynomial add(Polynomial p)



POSTCONDITION: this object and p are not changed
return a polynomial that is the sum of p and this polynomial



Here is my method



public Polynomial add( Polynomial p )

// use the copy constructor
Polynomial answer = new Polynomial( this );
// answer.termList.addAll(p.termList);
// use addAll()
//answer=this.termList.addAll(p.termList);
//ListIterator<Term> itr = answer.termList.listIterator();
//ListIterator<Term> itr = answer.termList.listIterator();
//ListIterator<Term> itr2 = p.termList.listIterator();
for ( ListIterator<Term> itr = answer.termList.listIterator(); itr.hasNext(); )

Term term = itr.next();

for ( ListIterator<Term> itr2 = p.termList.listIterator(); itr2.hasNext(); )

Term term2 = itr2.next();
if ( term.exponent == term2.exponent )

answer.itr.coefficient = answer.itr.coefficient + p.itr2.coefficient;


/**while ( itr.hasNext() )

Term term = itr.next();
**/
return answer;





This line for (ListIterator<Term> itr=answer.termList.listIterator();itr.hasNext();) and for(ListIterator<Term> itr2 = p.termList.listIterator(); itr2.hasNext();) have errors.



I keep getting an error that says itr and itr2 cannot be resolved or is not a field.



Here is part of the code that defines Polynomial there are a lot of methods which I didn't include since they are too long.



public class Polynomial implements Cloneable

// this is a nested static class, it defines a type
// all the instance varaibles can be access directly inside Polynomial
// class even though they have

// private modifier
private static class Term

private int exponent;
private double coefficient;

public Term( int exp, double coeff )

coefficient = coeff;
exponent = exp;



// instance variables of Polynomial
// first is the term of the Polynomial with the highest degree
private LinkedList<Term> termList;

/**
* Postcondition: Creates a polynomial which is 0.
**/
public Polynomial()

termList = new LinkedList<Term>();
termList.add( new Term( 0, 0 ) );


/**
* Postcondition: Creates a polynomial which has a single term
* a0*x^0
*
* @param a0 The value to be set as the coefficient of the
* constant (x^0) t erm.
**/
public Polynomial( double a0 )

termList = new LinkedList<Term>();
termList.add( new Term( 0, a0 ) );








java linked-list






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 6:49









Gayan Mettananda

1,100713




1,100713










asked Nov 13 '18 at 5:56









Preston Preston

33




33












  • Please post the code that defines Polynomial

    – mjuarez
    Nov 13 '18 at 5:58











  • Please indicate which line(s) has the errors

    – Scary Wombat
    Nov 13 '18 at 5:59






  • 1





    come-on, please make some effort to format your code

    – Scary Wombat
    Nov 13 '18 at 6:04











  • the termList member is declared private, isn’t this an issue in the add method? And please remove all code that has been commented out.

    – Joakim Danielson
    Nov 13 '18 at 6:08












  • @ScaryWombat what do you need?

    – Preston
    Nov 13 '18 at 6:09

















  • Please post the code that defines Polynomial

    – mjuarez
    Nov 13 '18 at 5:58











  • Please indicate which line(s) has the errors

    – Scary Wombat
    Nov 13 '18 at 5:59






  • 1





    come-on, please make some effort to format your code

    – Scary Wombat
    Nov 13 '18 at 6:04











  • the termList member is declared private, isn’t this an issue in the add method? And please remove all code that has been commented out.

    – Joakim Danielson
    Nov 13 '18 at 6:08












  • @ScaryWombat what do you need?

    – Preston
    Nov 13 '18 at 6:09
















Please post the code that defines Polynomial

– mjuarez
Nov 13 '18 at 5:58





Please post the code that defines Polynomial

– mjuarez
Nov 13 '18 at 5:58













Please indicate which line(s) has the errors

– Scary Wombat
Nov 13 '18 at 5:59





Please indicate which line(s) has the errors

– Scary Wombat
Nov 13 '18 at 5:59




1




1





come-on, please make some effort to format your code

– Scary Wombat
Nov 13 '18 at 6:04





come-on, please make some effort to format your code

– Scary Wombat
Nov 13 '18 at 6:04













the termList member is declared private, isn’t this an issue in the add method? And please remove all code that has been commented out.

– Joakim Danielson
Nov 13 '18 at 6:08






the termList member is declared private, isn’t this an issue in the add method? And please remove all code that has been commented out.

– Joakim Danielson
Nov 13 '18 at 6:08














@ScaryWombat what do you need?

– Preston
Nov 13 '18 at 6:09





@ScaryWombat what do you need?

– Preston
Nov 13 '18 at 6:09












2 Answers
2






active

oldest

votes


















0














This is the faulty line



answer.itr.coefficient= answer.itr.coefficient + p.itr2.coefficient;


itr and itr2 are iterator objects and not members of Polynomial, I guess what you meant was



term.coefficient += term2.coefficient;



Update



For completeness here is my full version of the add method



public Polynomial add(Polynomial p) 
Polynomial answer = new Polynomial(this);
for (ListIterator<Term> itr = answer.termList.listIterator(); itr.hasNext();)
Term term = itr.next();

for (ListIterator<Term> itr2 = p.termList.listIterator(); itr2.hasNext();)
Term term2 = itr2.next();
if (term.exponent == term2.exponent)
term.coefficient += term2.coefficient;
else
answer.termList.add(new Term(term2.exponent, term2.coefficient));



return answer;






share|improve this answer

























  • thanks! that solved that error! now its giving me another error method must return type Polynomial but I thought I declared answer as a Polynomial

    – Preston
    Nov 13 '18 at 6:36











  • @Preston I don't get that error so you must have done something else with the code.

    – Joakim Danielson
    Nov 13 '18 at 6:37












  • ok I fixed that.. Now I added an else statement after the if statement else answer.termList.add(new Term(term2.exponent,term2.coefficient));//exp,coeff*/ to add a node if term2 has an exponent that term1 does not have but I can't update the linked list while its iterating apparently I'm trying to figure out how to get around this

    – Preston
    Nov 13 '18 at 6:52











  • @Preston add new objects to a local ArrayList or similar and then add the content to termList after the iterator loops

    – Joakim Danielson
    Nov 13 '18 at 6:58











  • I'm just learning LinkedLists in class and I'm not familar with ArrayList would you mind showing me how to do that?

    – Preston
    Nov 13 '18 at 7:32



















0














The loops don't have to be nested.



This solution will keep the terms sorted by descending order of the exponent:



public class Polynomial 
private List<Term> termList = new LinkedList<Term>();

private static class Term
private int exponent;
private double coefficient;

private Term(int exponent, double coefficient)
this.exponent = exponent;
this.coefficient = coefficient;



public Polynomial()


public Polynomial(int exponent, double coeff)
if (coeff != 0)
termList.add(new Term(exponent, coeff));



public Polynomial add(Polynomial other)
Polynomial answer = new Polynomial();
Iterator<Term> itr = termList.iterator();
Iterator<Term> itr2 = other.termList.iterator();
Term t = itr.hasNext() ? itr.next() : null;
Term t2 = itr2.hasNext() ? itr2.next() : null;
while (true)
if (t == null)
if (t2 == null)
break;

answer.termList.add(new Term(t2.exponent, t2.coefficient));
t2 = itr2.hasNext() ? itr2.next() : null;
else if (t2 == null)
answer.termList.add(new Term(t.exponent, t.coefficient));
t = itr.hasNext() ? itr.next() : null;
else if (t2.exponent > t.exponent)
answer.termList.add(new Term(t2.exponent, t2.coefficient));
t2 = itr2.hasNext() ? itr2.next() : null;
else if (t2.exponent < t.exponent)
answer.termList.add(new Term(t.exponent, t.coefficient));
t = itr.hasNext() ? itr.next() : null;
else
answer.termList.add(
new Term(t.exponent, t.coefficient + t2.coefficient));
t = itr.hasNext() ? itr.next() : null;
t2 = itr2.hasNext() ? itr2.next() : null;


return answer;







share|improve this answer























  • when I tried to use your solution in main it gave me this ` Polynomial p4 = p2.add(p3);` this below is the output p2 = new Polynomial(): p2 = 3.0x^3 + -4.0x^4 p3 = new Polynomial(): p3 = -4.0x^4 + -4.0x^2 p4 = -4.0x^4 + 3.0x^3 + -4.0x^4 + -4.0x^2 +

    – Preston
    Nov 13 '18 at 7:26












  • @Preston the terms of the input polynomials must be sorted (and kept that way)

    – Maurice Perry
    Nov 13 '18 at 7:59










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%2f53274672%2fusing-nested-for-loop-to-add-2-linked-lists-with-java-util-listiterator%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














This is the faulty line



answer.itr.coefficient= answer.itr.coefficient + p.itr2.coefficient;


itr and itr2 are iterator objects and not members of Polynomial, I guess what you meant was



term.coefficient += term2.coefficient;



Update



For completeness here is my full version of the add method



public Polynomial add(Polynomial p) 
Polynomial answer = new Polynomial(this);
for (ListIterator<Term> itr = answer.termList.listIterator(); itr.hasNext();)
Term term = itr.next();

for (ListIterator<Term> itr2 = p.termList.listIterator(); itr2.hasNext();)
Term term2 = itr2.next();
if (term.exponent == term2.exponent)
term.coefficient += term2.coefficient;
else
answer.termList.add(new Term(term2.exponent, term2.coefficient));



return answer;






share|improve this answer

























  • thanks! that solved that error! now its giving me another error method must return type Polynomial but I thought I declared answer as a Polynomial

    – Preston
    Nov 13 '18 at 6:36











  • @Preston I don't get that error so you must have done something else with the code.

    – Joakim Danielson
    Nov 13 '18 at 6:37












  • ok I fixed that.. Now I added an else statement after the if statement else answer.termList.add(new Term(term2.exponent,term2.coefficient));//exp,coeff*/ to add a node if term2 has an exponent that term1 does not have but I can't update the linked list while its iterating apparently I'm trying to figure out how to get around this

    – Preston
    Nov 13 '18 at 6:52











  • @Preston add new objects to a local ArrayList or similar and then add the content to termList after the iterator loops

    – Joakim Danielson
    Nov 13 '18 at 6:58











  • I'm just learning LinkedLists in class and I'm not familar with ArrayList would you mind showing me how to do that?

    – Preston
    Nov 13 '18 at 7:32
















0














This is the faulty line



answer.itr.coefficient= answer.itr.coefficient + p.itr2.coefficient;


itr and itr2 are iterator objects and not members of Polynomial, I guess what you meant was



term.coefficient += term2.coefficient;



Update



For completeness here is my full version of the add method



public Polynomial add(Polynomial p) 
Polynomial answer = new Polynomial(this);
for (ListIterator<Term> itr = answer.termList.listIterator(); itr.hasNext();)
Term term = itr.next();

for (ListIterator<Term> itr2 = p.termList.listIterator(); itr2.hasNext();)
Term term2 = itr2.next();
if (term.exponent == term2.exponent)
term.coefficient += term2.coefficient;
else
answer.termList.add(new Term(term2.exponent, term2.coefficient));



return answer;






share|improve this answer

























  • thanks! that solved that error! now its giving me another error method must return type Polynomial but I thought I declared answer as a Polynomial

    – Preston
    Nov 13 '18 at 6:36











  • @Preston I don't get that error so you must have done something else with the code.

    – Joakim Danielson
    Nov 13 '18 at 6:37












  • ok I fixed that.. Now I added an else statement after the if statement else answer.termList.add(new Term(term2.exponent,term2.coefficient));//exp,coeff*/ to add a node if term2 has an exponent that term1 does not have but I can't update the linked list while its iterating apparently I'm trying to figure out how to get around this

    – Preston
    Nov 13 '18 at 6:52











  • @Preston add new objects to a local ArrayList or similar and then add the content to termList after the iterator loops

    – Joakim Danielson
    Nov 13 '18 at 6:58











  • I'm just learning LinkedLists in class and I'm not familar with ArrayList would you mind showing me how to do that?

    – Preston
    Nov 13 '18 at 7:32














0












0








0







This is the faulty line



answer.itr.coefficient= answer.itr.coefficient + p.itr2.coefficient;


itr and itr2 are iterator objects and not members of Polynomial, I guess what you meant was



term.coefficient += term2.coefficient;



Update



For completeness here is my full version of the add method



public Polynomial add(Polynomial p) 
Polynomial answer = new Polynomial(this);
for (ListIterator<Term> itr = answer.termList.listIterator(); itr.hasNext();)
Term term = itr.next();

for (ListIterator<Term> itr2 = p.termList.listIterator(); itr2.hasNext();)
Term term2 = itr2.next();
if (term.exponent == term2.exponent)
term.coefficient += term2.coefficient;
else
answer.termList.add(new Term(term2.exponent, term2.coefficient));



return answer;






share|improve this answer















This is the faulty line



answer.itr.coefficient= answer.itr.coefficient + p.itr2.coefficient;


itr and itr2 are iterator objects and not members of Polynomial, I guess what you meant was



term.coefficient += term2.coefficient;



Update



For completeness here is my full version of the add method



public Polynomial add(Polynomial p) 
Polynomial answer = new Polynomial(this);
for (ListIterator<Term> itr = answer.termList.listIterator(); itr.hasNext();)
Term term = itr.next();

for (ListIterator<Term> itr2 = p.termList.listIterator(); itr2.hasNext();)
Term term2 = itr2.next();
if (term.exponent == term2.exponent)
term.coefficient += term2.coefficient;
else
answer.termList.add(new Term(term2.exponent, term2.coefficient));



return answer;







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 13 '18 at 8:03

























answered Nov 13 '18 at 6:24









Joakim DanielsonJoakim Danielson

8,3223724




8,3223724












  • thanks! that solved that error! now its giving me another error method must return type Polynomial but I thought I declared answer as a Polynomial

    – Preston
    Nov 13 '18 at 6:36











  • @Preston I don't get that error so you must have done something else with the code.

    – Joakim Danielson
    Nov 13 '18 at 6:37












  • ok I fixed that.. Now I added an else statement after the if statement else answer.termList.add(new Term(term2.exponent,term2.coefficient));//exp,coeff*/ to add a node if term2 has an exponent that term1 does not have but I can't update the linked list while its iterating apparently I'm trying to figure out how to get around this

    – Preston
    Nov 13 '18 at 6:52











  • @Preston add new objects to a local ArrayList or similar and then add the content to termList after the iterator loops

    – Joakim Danielson
    Nov 13 '18 at 6:58











  • I'm just learning LinkedLists in class and I'm not familar with ArrayList would you mind showing me how to do that?

    – Preston
    Nov 13 '18 at 7:32


















  • thanks! that solved that error! now its giving me another error method must return type Polynomial but I thought I declared answer as a Polynomial

    – Preston
    Nov 13 '18 at 6:36











  • @Preston I don't get that error so you must have done something else with the code.

    – Joakim Danielson
    Nov 13 '18 at 6:37












  • ok I fixed that.. Now I added an else statement after the if statement else answer.termList.add(new Term(term2.exponent,term2.coefficient));//exp,coeff*/ to add a node if term2 has an exponent that term1 does not have but I can't update the linked list while its iterating apparently I'm trying to figure out how to get around this

    – Preston
    Nov 13 '18 at 6:52











  • @Preston add new objects to a local ArrayList or similar and then add the content to termList after the iterator loops

    – Joakim Danielson
    Nov 13 '18 at 6:58











  • I'm just learning LinkedLists in class and I'm not familar with ArrayList would you mind showing me how to do that?

    – Preston
    Nov 13 '18 at 7:32

















thanks! that solved that error! now its giving me another error method must return type Polynomial but I thought I declared answer as a Polynomial

– Preston
Nov 13 '18 at 6:36





thanks! that solved that error! now its giving me another error method must return type Polynomial but I thought I declared answer as a Polynomial

– Preston
Nov 13 '18 at 6:36













@Preston I don't get that error so you must have done something else with the code.

– Joakim Danielson
Nov 13 '18 at 6:37






@Preston I don't get that error so you must have done something else with the code.

– Joakim Danielson
Nov 13 '18 at 6:37














ok I fixed that.. Now I added an else statement after the if statement else answer.termList.add(new Term(term2.exponent,term2.coefficient));//exp,coeff*/ to add a node if term2 has an exponent that term1 does not have but I can't update the linked list while its iterating apparently I'm trying to figure out how to get around this

– Preston
Nov 13 '18 at 6:52





ok I fixed that.. Now I added an else statement after the if statement else answer.termList.add(new Term(term2.exponent,term2.coefficient));//exp,coeff*/ to add a node if term2 has an exponent that term1 does not have but I can't update the linked list while its iterating apparently I'm trying to figure out how to get around this

– Preston
Nov 13 '18 at 6:52













@Preston add new objects to a local ArrayList or similar and then add the content to termList after the iterator loops

– Joakim Danielson
Nov 13 '18 at 6:58





@Preston add new objects to a local ArrayList or similar and then add the content to termList after the iterator loops

– Joakim Danielson
Nov 13 '18 at 6:58













I'm just learning LinkedLists in class and I'm not familar with ArrayList would you mind showing me how to do that?

– Preston
Nov 13 '18 at 7:32






I'm just learning LinkedLists in class and I'm not familar with ArrayList would you mind showing me how to do that?

– Preston
Nov 13 '18 at 7:32














0














The loops don't have to be nested.



This solution will keep the terms sorted by descending order of the exponent:



public class Polynomial 
private List<Term> termList = new LinkedList<Term>();

private static class Term
private int exponent;
private double coefficient;

private Term(int exponent, double coefficient)
this.exponent = exponent;
this.coefficient = coefficient;



public Polynomial()


public Polynomial(int exponent, double coeff)
if (coeff != 0)
termList.add(new Term(exponent, coeff));



public Polynomial add(Polynomial other)
Polynomial answer = new Polynomial();
Iterator<Term> itr = termList.iterator();
Iterator<Term> itr2 = other.termList.iterator();
Term t = itr.hasNext() ? itr.next() : null;
Term t2 = itr2.hasNext() ? itr2.next() : null;
while (true)
if (t == null)
if (t2 == null)
break;

answer.termList.add(new Term(t2.exponent, t2.coefficient));
t2 = itr2.hasNext() ? itr2.next() : null;
else if (t2 == null)
answer.termList.add(new Term(t.exponent, t.coefficient));
t = itr.hasNext() ? itr.next() : null;
else if (t2.exponent > t.exponent)
answer.termList.add(new Term(t2.exponent, t2.coefficient));
t2 = itr2.hasNext() ? itr2.next() : null;
else if (t2.exponent < t.exponent)
answer.termList.add(new Term(t.exponent, t.coefficient));
t = itr.hasNext() ? itr.next() : null;
else
answer.termList.add(
new Term(t.exponent, t.coefficient + t2.coefficient));
t = itr.hasNext() ? itr.next() : null;
t2 = itr2.hasNext() ? itr2.next() : null;


return answer;







share|improve this answer























  • when I tried to use your solution in main it gave me this ` Polynomial p4 = p2.add(p3);` this below is the output p2 = new Polynomial(): p2 = 3.0x^3 + -4.0x^4 p3 = new Polynomial(): p3 = -4.0x^4 + -4.0x^2 p4 = -4.0x^4 + 3.0x^3 + -4.0x^4 + -4.0x^2 +

    – Preston
    Nov 13 '18 at 7:26












  • @Preston the terms of the input polynomials must be sorted (and kept that way)

    – Maurice Perry
    Nov 13 '18 at 7:59















0














The loops don't have to be nested.



This solution will keep the terms sorted by descending order of the exponent:



public class Polynomial 
private List<Term> termList = new LinkedList<Term>();

private static class Term
private int exponent;
private double coefficient;

private Term(int exponent, double coefficient)
this.exponent = exponent;
this.coefficient = coefficient;



public Polynomial()


public Polynomial(int exponent, double coeff)
if (coeff != 0)
termList.add(new Term(exponent, coeff));



public Polynomial add(Polynomial other)
Polynomial answer = new Polynomial();
Iterator<Term> itr = termList.iterator();
Iterator<Term> itr2 = other.termList.iterator();
Term t = itr.hasNext() ? itr.next() : null;
Term t2 = itr2.hasNext() ? itr2.next() : null;
while (true)
if (t == null)
if (t2 == null)
break;

answer.termList.add(new Term(t2.exponent, t2.coefficient));
t2 = itr2.hasNext() ? itr2.next() : null;
else if (t2 == null)
answer.termList.add(new Term(t.exponent, t.coefficient));
t = itr.hasNext() ? itr.next() : null;
else if (t2.exponent > t.exponent)
answer.termList.add(new Term(t2.exponent, t2.coefficient));
t2 = itr2.hasNext() ? itr2.next() : null;
else if (t2.exponent < t.exponent)
answer.termList.add(new Term(t.exponent, t.coefficient));
t = itr.hasNext() ? itr.next() : null;
else
answer.termList.add(
new Term(t.exponent, t.coefficient + t2.coefficient));
t = itr.hasNext() ? itr.next() : null;
t2 = itr2.hasNext() ? itr2.next() : null;


return answer;







share|improve this answer























  • when I tried to use your solution in main it gave me this ` Polynomial p4 = p2.add(p3);` this below is the output p2 = new Polynomial(): p2 = 3.0x^3 + -4.0x^4 p3 = new Polynomial(): p3 = -4.0x^4 + -4.0x^2 p4 = -4.0x^4 + 3.0x^3 + -4.0x^4 + -4.0x^2 +

    – Preston
    Nov 13 '18 at 7:26












  • @Preston the terms of the input polynomials must be sorted (and kept that way)

    – Maurice Perry
    Nov 13 '18 at 7:59













0












0








0







The loops don't have to be nested.



This solution will keep the terms sorted by descending order of the exponent:



public class Polynomial 
private List<Term> termList = new LinkedList<Term>();

private static class Term
private int exponent;
private double coefficient;

private Term(int exponent, double coefficient)
this.exponent = exponent;
this.coefficient = coefficient;



public Polynomial()


public Polynomial(int exponent, double coeff)
if (coeff != 0)
termList.add(new Term(exponent, coeff));



public Polynomial add(Polynomial other)
Polynomial answer = new Polynomial();
Iterator<Term> itr = termList.iterator();
Iterator<Term> itr2 = other.termList.iterator();
Term t = itr.hasNext() ? itr.next() : null;
Term t2 = itr2.hasNext() ? itr2.next() : null;
while (true)
if (t == null)
if (t2 == null)
break;

answer.termList.add(new Term(t2.exponent, t2.coefficient));
t2 = itr2.hasNext() ? itr2.next() : null;
else if (t2 == null)
answer.termList.add(new Term(t.exponent, t.coefficient));
t = itr.hasNext() ? itr.next() : null;
else if (t2.exponent > t.exponent)
answer.termList.add(new Term(t2.exponent, t2.coefficient));
t2 = itr2.hasNext() ? itr2.next() : null;
else if (t2.exponent < t.exponent)
answer.termList.add(new Term(t.exponent, t.coefficient));
t = itr.hasNext() ? itr.next() : null;
else
answer.termList.add(
new Term(t.exponent, t.coefficient + t2.coefficient));
t = itr.hasNext() ? itr.next() : null;
t2 = itr2.hasNext() ? itr2.next() : null;


return answer;







share|improve this answer













The loops don't have to be nested.



This solution will keep the terms sorted by descending order of the exponent:



public class Polynomial 
private List<Term> termList = new LinkedList<Term>();

private static class Term
private int exponent;
private double coefficient;

private Term(int exponent, double coefficient)
this.exponent = exponent;
this.coefficient = coefficient;



public Polynomial()


public Polynomial(int exponent, double coeff)
if (coeff != 0)
termList.add(new Term(exponent, coeff));



public Polynomial add(Polynomial other)
Polynomial answer = new Polynomial();
Iterator<Term> itr = termList.iterator();
Iterator<Term> itr2 = other.termList.iterator();
Term t = itr.hasNext() ? itr.next() : null;
Term t2 = itr2.hasNext() ? itr2.next() : null;
while (true)
if (t == null)
if (t2 == null)
break;

answer.termList.add(new Term(t2.exponent, t2.coefficient));
t2 = itr2.hasNext() ? itr2.next() : null;
else if (t2 == null)
answer.termList.add(new Term(t.exponent, t.coefficient));
t = itr.hasNext() ? itr.next() : null;
else if (t2.exponent > t.exponent)
answer.termList.add(new Term(t2.exponent, t2.coefficient));
t2 = itr2.hasNext() ? itr2.next() : null;
else if (t2.exponent < t.exponent)
answer.termList.add(new Term(t.exponent, t.coefficient));
t = itr.hasNext() ? itr.next() : null;
else
answer.termList.add(
new Term(t.exponent, t.coefficient + t2.coefficient));
t = itr.hasNext() ? itr.next() : null;
t2 = itr2.hasNext() ? itr2.next() : null;


return answer;








share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 13 '18 at 7:03









Maurice PerryMaurice Perry

5,0002515




5,0002515












  • when I tried to use your solution in main it gave me this ` Polynomial p4 = p2.add(p3);` this below is the output p2 = new Polynomial(): p2 = 3.0x^3 + -4.0x^4 p3 = new Polynomial(): p3 = -4.0x^4 + -4.0x^2 p4 = -4.0x^4 + 3.0x^3 + -4.0x^4 + -4.0x^2 +

    – Preston
    Nov 13 '18 at 7:26












  • @Preston the terms of the input polynomials must be sorted (and kept that way)

    – Maurice Perry
    Nov 13 '18 at 7:59

















  • when I tried to use your solution in main it gave me this ` Polynomial p4 = p2.add(p3);` this below is the output p2 = new Polynomial(): p2 = 3.0x^3 + -4.0x^4 p3 = new Polynomial(): p3 = -4.0x^4 + -4.0x^2 p4 = -4.0x^4 + 3.0x^3 + -4.0x^4 + -4.0x^2 +

    – Preston
    Nov 13 '18 at 7:26












  • @Preston the terms of the input polynomials must be sorted (and kept that way)

    – Maurice Perry
    Nov 13 '18 at 7:59
















when I tried to use your solution in main it gave me this ` Polynomial p4 = p2.add(p3);` this below is the output p2 = new Polynomial(): p2 = 3.0x^3 + -4.0x^4 p3 = new Polynomial(): p3 = -4.0x^4 + -4.0x^2 p4 = -4.0x^4 + 3.0x^3 + -4.0x^4 + -4.0x^2 +

– Preston
Nov 13 '18 at 7:26






when I tried to use your solution in main it gave me this ` Polynomial p4 = p2.add(p3);` this below is the output p2 = new Polynomial(): p2 = 3.0x^3 + -4.0x^4 p3 = new Polynomial(): p3 = -4.0x^4 + -4.0x^2 p4 = -4.0x^4 + 3.0x^3 + -4.0x^4 + -4.0x^2 +

– Preston
Nov 13 '18 at 7:26














@Preston the terms of the input polynomials must be sorted (and kept that way)

– Maurice Perry
Nov 13 '18 at 7:59





@Preston the terms of the input polynomials must be sorted (and kept that way)

– Maurice Perry
Nov 13 '18 at 7:59

















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%2f53274672%2fusing-nested-for-loop-to-add-2-linked-lists-with-java-util-listiterator%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

How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

Syphilis

Darth Vader #20