当我构造第二个新的多项式(链表)类时,第一个类采用第二个类的值
package polynomial;
/**
*
* @author Steven
*/
public class Polynomial
{
private float data;
protected static Polynomial head;
private Polynomial link;
/**
* @param args the command line arguments
*/
public Polynomial(float[] data)
{
head = null;
if(data.length == 1)
head = insertAtFront(head, data[0]);
for(int i = data.length-1; i >= 0; i--)
{
System.out.println(head);
head = insertAtFront(head, data[i]);
}
}
public Polynomial(float data, Polynomial link)
{
this.link = link;
this.data = data;
}
public static Polynomial add(Polynomial p, Polynomial p2)
{
if(p.length() > p2.length())
{
while(p.length() != p2.length())
{
insertAtFront(p2, 0);
}
for(Polynomial poly = p; poly != null && p2 != null; poly = poly.link)
{
p2.data = (p2.data + poly.data);
p2 = p2.link;
}
}
else if(p2.length() > p.length())
{
while(p2.length() != p.length())
{
insertAtFront(p, 0);
}
for(Polynomial poly = p; poly != null && p2 != null; poly = poly.link)
{
p2.data = (p2.data + poly.data);
p2 = p2.link;
}
}
else
{
for(Polynomial poly = p; poly != null && p2 != null; poly = poly.link)
{
p2.data = (p2.data + poly.data);
p2 = p2.link;
}
}
return p2.head;
}
public float evaluate(float x)
{
int i = head.length()-1;
float y = 0;
for(Polynomial poly = head; poly != null; poly = poly.link)
{
if(poly.link == null)
y += poly.data;
else
y += (poly.data * (float)(Math.pow(x, i)));
i -= 1;
}
return y;
}
@Override
public String toString()
{
int i = 1;
String polyString = "blank";
for(Polynomial poly = head; poly != null; poly = poly.link)
{
if(polyString.equalsIgnoreCase("blank"))
{
if(poly.data != 0)
polyString = poly.data + "x^" + (poly.length()-i) + " + ";
else if(poly.data == 1)
polyString = "x^" + (poly.length()-i) + " + ";
else
polyString = poly.data + "x^" + (poly.length()-i) + " + ";
}
else
{
if(poly.link == null)
{
if(poly.data != 0)
polyString = polyString + poly.data;
else if(poly.data == 1)
polyString = polyString + "x";
else
polyString = polyString + poly.data + "x^" + (poly.length()-i);
}
else
{
if(poly.data != 0)
polyString = polyString + poly.data + "x^" + (poly.length()-i) + " + ";
else if(poly.data == 1)
polyString = polyString + "x^" + (poly.length()-i) + " + ";
else
;
}
}
i = i + 1;
}
return polyString;
}
public int length()
{
int answer = 0;
for(Polynomial poly = head; poly != null; poly = poly.link)
{
answer++;
}
return answer;
}
private static Polynomial insertAtFront(Polynomial head, float data)
{
return new Polynomial(data, head);
}
}
我有第二个类,它只是一个运行这些方法的程序,当我创建一个新的多项式并运行关联的方法时,一切似乎都运行成功。创建新多项式后,第一个多项式采用第二个多项式的值。我不明白 1. 第一个创建的多项式的值将去往何处,以及 2. 为什么要这样做。
package polynomial;
/**
*
* @author Steven
*/
public class Polynomial
{
private float data;
protected static Polynomial head;
private Polynomial link;
/**
* @param args the command line arguments
*/
public Polynomial(float[] data)
{
head = null;
if(data.length == 1)
head = insertAtFront(head, data[0]);
for(int i = data.length-1; i >= 0; i--)
{
System.out.println(head);
head = insertAtFront(head, data[i]);
}
}
public Polynomial(float data, Polynomial link)
{
this.link = link;
this.data = data;
}
public static Polynomial add(Polynomial p, Polynomial p2)
{
if(p.length() > p2.length())
{
while(p.length() != p2.length())
{
insertAtFront(p2, 0);
}
for(Polynomial poly = p; poly != null && p2 != null; poly = poly.link)
{
p2.data = (p2.data + poly.data);
p2 = p2.link;
}
}
else if(p2.length() > p.length())
{
while(p2.length() != p.length())
{
insertAtFront(p, 0);
}
for(Polynomial poly = p; poly != null && p2 != null; poly = poly.link)
{
p2.data = (p2.data + poly.data);
p2 = p2.link;
}
}
else
{
for(Polynomial poly = p; poly != null && p2 != null; poly = poly.link)
{
p2.data = (p2.data + poly.data);
p2 = p2.link;
}
}
return p2.head;
}
public float evaluate(float x)
{
int i = head.length()-1;
float y = 0;
for(Polynomial poly = head; poly != null; poly = poly.link)
{
if(poly.link == null)
y += poly.data;
else
y += (poly.data * (float)(Math.pow(x, i)));
i -= 1;
}
return y;
}
@Override
public String toString()
{
int i = 1;
String polyString = "blank";
for(Polynomial poly = head; poly != null; poly = poly.link)
{
if(polyString.equalsIgnoreCase("blank"))
{
if(poly.data != 0)
polyString = poly.data + "x^" + (poly.length()-i) + " + ";
else if(poly.data == 1)
polyString = "x^" + (poly.length()-i) + " + ";
else
polyString = poly.data + "x^" + (poly.length()-i) + " + ";
}
else
{
if(poly.link == null)
{
if(poly.data != 0)
polyString = polyString + poly.data;
else if(poly.data == 1)
polyString = polyString + "x";
else
polyString = polyString + poly.data + "x^" + (poly.length()-i);
}
else
{
if(poly.data != 0)
polyString = polyString + poly.data + "x^" + (poly.length()-i) + " + ";
else if(poly.data == 1)
polyString = polyString + "x^" + (poly.length()-i) + " + ";
else
;
}
}
i = i + 1;
}
return polyString;
}
public int length()
{
int answer = 0;
for(Polynomial poly = head; poly != null; poly = poly.link)
{
answer++;
}
return answer;
}
private static Polynomial insertAtFront(Polynomial head, float data)
{
return new Polynomial(data, head);
}
}
I have a second class that is just a program to run these methods, and when I create a new Polynomial, and run associated methods, everything appears to run successfully. After I create a new Polynomial, the first Polynomial takes the second one's values. I don't understand 1. where the first-created polynomial's values are going and 2. why it's doing this at all.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的
head
被声明为static
- 因此所有类都只有一份它的副本!创建第二个多项式
后,它将覆盖第一个多项式的头部。您可能希望它是实例变量而不是类变量。
但是,这样做也会带来无限递归构造函数调用的问题。为了克服这个问题,您可能需要一个不同的类,该类将多项式头作为实例变量,并且每个列表仅初始化一次。
your
head
is declared asstatic
- thus there is only one copy of it for all classes! Once your secondPolynomial
is created, it overrides the head of the first one.You probably want it to be an instance variable and not a class variable.
Though, doing it will also have its own problems of infinite recursive constructor invokation. To overcome it, you might want to have a different class that holds a
Polynomial head
as an instance variable, and initialized it only once per list.