【问题标题】:how to send a one dimensional array to a class and how to write a method in main that copies it-for beginners java如何将一维数组发送到一个类以及如何在 main 中编写一个复制它的方法 - 对于初学者 java
【发布时间】:2017-06-15 23:20:37
【问题描述】:

我正在尝试向班级发送一个 D 数组。我希望 mutator 方法复制数组,保存它,然后将其发送回我的测试器类......到目前为止,我不断收到来自编译器的错误。我知道如何编写一个在 main 中复制数组的方法,但我似乎无法用类完成它。代码如下:

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);

    }
    }
}

【问题讨论】:

  • 你说你从编译器得到错误 - 你能具体点吗?
  • 它一直说类接口或枚举预期......我也不知道如何让一个类复制一个数组。请对新的计算机科学家有点同情......
  • 如果我没有遗憾,我会忽略这个问题 :) 我注意到该代码中的第一行 public Class1D { 似乎无效。不应该是public class Class1D {吗?
  • 是的,继续取笑我的多动症,我以某种方式修复了它
  • 我真的不是在开玩笑。像这样的小事情会使整个事情无法编译。

标签: java arrays class


【解决方案1】:

您应该将a 数组发送到 Class1D 的构造函数,并将其设置为您的成员变量,如下所示:

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);

【讨论】:

  • 我想我可能需要编辑这个问题。我只是写了复制方法来表明我知道如何在你看到的 main 中复制数组……我真正想做的是将数组发送到我创建的类并让类进行复制……我不想在 main 中制作副本
  • 得到你,改变我的答案。
  • 谢谢。我是 Java 新手,上课时遇到困难
  • @therdfox24 不要害怕,这很简单 :)
  • 谢谢,我现在就试试这个
【解决方案2】:
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;

    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多