0

So I have this class of terms

public class Term {
private int coefficient ;
private int exponent ;
public Term(int coefficient, int exponent)
{
 this.coefficient = coefficient ;
 this.exponent = exponent ;
}
public int getCo()
{
 return coefficient ;
}
public int getEx()
{
 return exponent ;
}
}

And I have a class called Polynomial that creates an arraylist of type term with a method called delete that isn't working the way I'd like it to and I can't figure out why:

public class Polynomial{
public Polynomial()
{
 poly = new ArrayList<>() ;
}
public void delete (int coeff, int expo)
{
 for(int i = 0; i < poly.size(); i++) 
 {
 System.out.println("**Delete Loop works") ; //This does not print
 if( (poly.get(i).getCo() == coeff) && (poly.get(i).getEx() == expo) ) 
 {
 poly.remove(i) ;
 removed = true ; 
 break ;
 }
 }
}
}

It keeps throwing an exception every time I run the delete method and I'm not sure why?? I am using input from a text file (and the input is working fine), I think it might be going wrong where I use : poly.get(i).getCo() == coeff is that properly written?

Thanks for any help you can provide!

asked Dec 2, 2013 at 5:20
8
  • What is the exception? Commented Dec 2, 2013 at 5:21
  • where I wrote //this does not print it doesn't even print if I write a loop that is always true. It is strange. Commented Dec 2, 2013 at 5:23
  • If that SOP does not print, then it means the poly arraylist is empty and has nothing to be deleted from it. Commented Dec 2, 2013 at 5:23
  • I think your ArrayList is empty. Commented Dec 2, 2013 at 5:23
  • at Polynomial.delete(Polynomial.java:72) at PolynomialTest.main(PolynomialTest.java:49) Java Result: 1 Commented Dec 2, 2013 at 5:24

2 Answers 2

1

In your Polynomial constructor, you have defined poly = new ArrayList<>() ; which creates ArrayList with not a single object in it.

That means poly.size() = 0 that's why it will not enter in loop and your syso statement will not executed.

answered Dec 2, 2013 at 5:23
Sign up to request clarification or add additional context in comments.

3 Comments

I'm using another class with a main method in it to populate the list. :<
-1 The OP specifically said he is having an exception. If size is 0 there no reason for occurring an exception.
0

It keeps throwing an exception every time I run the delete method and I'm not sure why??

You are probably having ConcurrentModificationException. The exception is thrown because you are iterating over the array list and removing an element at the same time. Try using Iterator<T>iterator instead and iterator.remove() method.

 Iterator<Poly>iterator = arrayList.iterator();
 while(iterator.hasNext())
 {
 Poly poly = iterator.next();
 if( (poly.getCo() == coeff) && (poly.getEx() == expo) ) 
 iterator.remove();
 }
answered Dec 2, 2013 at 5:30

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.