#/help

17 messages · Page 1 of 1 (latest)

subtle patio
#

I'm trying to create a program which represents a polynomial.
The class named "Polynom".

In order to create the polynomial which is a Pair<Double, Integer>, the constructor get 2 parameters, parameter of ArrayList<Double> represents the coefficients and parameter of ArrayList<Integer> represents the powers of the polynomial.

For example:
Polynomial P is P = 10x^3 + 8x^2 + 5x^1
Coefficients are: 10, 8 , 5
Powers are: 3, 2, 1

If the 2 parameters are different in size, the constructor will throw an Exception.

The question is:
Why it doesn't work?
In the main it doesn't create the polynomial even though the the ArrayLists are identical in size.. and instead it goes into the catch block and "turns on" the exception.
I marked the problematic line in the main in yellow, please help me, thank you!!

P.S:
Forgot to say, I created a private method which organizes the 2 arrays in decreasing order relative to the powers' ArrayList (So in the polynomial the highest power will be first and the lowest power will be last when I presents the polynomial)

ripe gorgeBOT
#

This post has been reserved for your question.

Hey @subtle patio! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

pulsar trout
subtle patio
#

@pulsar trout

pulsar trout
#

You are using an iterator to iterate over the powers arraylist. While you are iterating over it every change to the underlying arraylist will result in a ConcurrentModificationException. Convert the inner for loop in your sortArray method to a traditional for loop

subtle patio
ripe gorgeBOT
pulsar trout
#

Its not a bug. Its just a precaution the ArrayList takes. ArrayList is not synchronized meaning that if two threads were to manipulate it at the same time the behavior is undefined. As a precaution tbe developers added a system that not always but most of the time throws an exception when that occurs, letting the developer know that what he was doing caused undefined behavior.
A for-each loop is basically just syntax sugar for the following

for(Iterator<Integer> iter = list.iterator(); iter.hasNext(); ){
    int element = iter.next();
}

Only difference is that you do not have access to the iterator that was created but only to the elements it returns. When you change the ArrayList while you are using the iterator (without using the iterators methods for modifying) you might end up in a state where a call to next returns null or skips an element or worse throws an exception. To prevent that from happening the ArrayList to the best of its ability throws a ConcurrentModificationException letting the programmer know that what he was doing resulted in possibly undefined behavior.

#

Btw you might be able to simplify your code a lot if you first create the pairs and add them to your arraylist and then sort the arraylist with Collections.sort

subtle patio
ripe gorgeBOT
subtle patio
#

Is there a way I can save this post so I don't lose it?

#

Before i'll close it

pulsar trout
#

You can copy the link to this post. And if you close it, it'll just get placed further down not deleted so you can always search for it.

subtle patio
#

Done
Thank you again ❤️

ripe gorgeBOT
# subtle patio Done Thank you again ❤️

If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.