如何向类发送一维数组,以及如何在main中编写复制该数组的方法供java初学者使用
我正在尝试向类发送一个一维数组。我希望mutator方法复制数组,保存它,然后将它发送回我的tester类…到目前为止,我一直从编译器中得到错误。我知道如何编写一个方法来复制main中的数组,但我似乎无法用这个类完成它。代码如下:如何向类发送一维数组,以及如何在main中编写复制该数组的方法供java初学者使用,java,arrays,class,Java,Arrays,Class,我正在尝试向类发送一个一维数组。我希望mutator方法复制数组,保存它,然后将它发送回我的tester类…到目前为止,我一直从编译器中得到错误。我知道如何编写一个方法来复制main中的数组,但我似乎无法用这个类完成它。代码如下: public class Class1D { private int degree; private int [] coefficient; public Class1D(int degree){ this.degree =degree;
public class Class1D {
private int degree;
private int [] coefficient;
public Class1D(int degree){
this.degree =degree;
}
public void setCoefficent( int[] a, int degree){
this.coefficient[degree] = a[degree];
for ( int i=0; i<=a.length-1; i++)
{
this.coefficient[i] = a[i];
}
}
public int [] getCoefficient() {
return coefficient;
}
}
import javax.swing.JOptionPane;
public class copy1D{
public static void main(String[]args)
{
String input;
int degree;
input = JOptionPane.showInputDialog(" what is the degree of the polynomial?");
degree = Integer.parseInt(input);
degree= degree+1;
int [] array = new int[degree];
for ( int i =0; i<=array.length-1; i++)
{
input = JOptionPane.showInputDialog(" Enter coefficients:");
array[i] = Integer.parseInt(input);
}
for ( int i =0; i<=array.length-1; i++)
{
System.out.print(array[i] + " ");
}
Class1D array2 = new Class1D(degree);
}
}
}
公共类ClassID{
私人智力学位;
私有int[]系数;
公共D级(国际学位){
这个。度=度;
}
公共无效集合系数(int[]a,int度){
这个系数[度]=a[度];
对于(int i=0;i,您应该将a
数组发送给ClassID的构造函数,并将其设置为您的成员变量,如下所示:
public Class1D(int degree, int[] a){
this.degree =degree;
this.coefficient = a.clone();
// You can also another way that is faster, use:
// System.arraycopy(a, 0, this.coefficient, 0, a.length);
}
你对施工的要求是:
Class1D c1d = new Class1D(degree, array);
import javax.swing.JOptionPane;
公共类copy1D{
公共静态void main(字符串[]args)
{
字符串输入;
智力度;
input=JOptionPane.showInputDialog(“多项式的阶数是多少?”);
度=整数.parseInt(输入);
度=度+1;
int[]数组=新的int[度];
对于(int i=0;我想我可能需要编辑这个问题。我只是编写了copy方法,以表明我知道如何在main中复制数组,你看……我实际上想做的是将数组发送到我创建的类,并让该类进行复制……我不想在main中进行复制。明白了,更改了我的答案。谢谢。我是java a新手和你在一起很艰难classes@theredfox24别害怕,很简单:)谢谢,我现在就来试试。你说你从编译器那里得到了错误,你能说得具体点吗?它一直说需要类接口或枚举…而且我也不知道如何让类复制数组。请同情一下新的计算机科学家……如果我没有同情心,我会忽略这个问题:)我注意到代码中的第一行,public classad{
,似乎是无效的。那不应该是public classad{
?是的,继续,拿我的ADHD开点玩笑吧,我用一种我不是在开玩笑的方式修复了它。像这样的小事情会让整个事情无法编译。
import javax.swing.JOptionPane;
public class copy1D{
public static void main(String[]args)
{
String input;
int degree;
input = JOptionPane.showInputDialog(" what is the degree of the polynomial?");
degree = Integer.parseInt(input);
degree= degree+1;
int [] array = new int[degree];
for ( int i =0; i<=array.length-1; i++)
{
input = JOptionPane.showInputDialog(" Enter coefficients:");
array[i] = Integer.parseInt(input);
}
for ( int i =0; i<=array.length-1; i++)
{
System.out.print(array[i] + " ");
}
makecopy(array,degree);
Class1D c1d = new Class1D(degree, array);
}
public static void makecopy(int[]a, int deg)
{
int [] b = new int [deg];
for ( int i =0; i<=a.length-1; i++)
{
b[i] = a[i];
}
System.out.println(" I have copied the array ");
for ( int i =0; i<=a.length-1; i++)
{
System.out.print(b[i]+ " ");
}
}
}
public class Class1D {
private int degree;
private int [] coefficient;
public Class1D(int degree){
this.degree =degree;
}
public Class1D(int degree, int[] a){
this.degree =degree;
this.coefficient = a.clone();
}
public int []getCoefficient() {
return coefficient;
}
}