java - 如果我尝试将数组作为参数传递到不同的公共(public)类中,为什么我的构造函数无法在 Java 中编译?

标签 java arrays class constructor public

我正在尝试编译此代码,但不断收到错误 - 错误:/Users/Pedro/Dropbox/school/java/Proj/exam 3/EchoText.java:23:找不到符号 符号:构造函数 MultiPoint(java.lang.Double[]) 位置:多点类

如果我在公共(public) EchoText 的构造函数中省略数组,它将进行编译。但是,我需要它将数组作为参数传递到 MultiPoint 公共(public)类中。我的问题是为什么它不能与参数一起编译以及这个错误到底意味着什么?

public class MultiPoint
{
    private double[] testCoord;

    public MultiPoint(double[] coordPt)
    {
      testCoord = coordPt;
    }

    public void printPoints()
    {
        for ( int i = 0; i < coordPt.length; i++ )
        {
            System.out.println("Coordinate # " + i + " : " + coordPt[ i ]);
        }
        System.out.println("");
    }
}

import java.util.Scanner;
import java.lang.Math;
import java.util.ArrayList;

public class EchoText
{
  private static String ans = null;

  public static double number, coord;
  private static int counter =0;
  private static ArrayList <Double> coordList = new ArrayList<Double>();
  public static void main(String[] args)
  {

    mainDeal(); 
    Double coordPt[] = new Double[ coordList.size() ];
    coordList.toArray(coordPt);

    for (Double list1 : coordPt)
    {
      System.out.println((counter + 1) + list1);
    }   
    MultiPoint myMultiPoint = new MultiPoint(coordPt);
  }
   // HELP MENU CREATED BY PROF. BLANDO! I DO NOT TAKE CREDIT FOR THE HELP MENU TEXT!
   public static void helpMenu()
   {
     System.out.println("****************************** LAB 7 HELP MENU ******************************");
     System.out.println("\t\t\tBy Pedro Estrada");
     System.out.println("* This program defines a point in a N-Dimensional space. ");
     System.out.println("    - Each point can have different number of non-zero coordinate");
     System.out.println("    - You may request a random number for any coordinate by typing \"RANDOM\"");
     System.out.println("    - When you are finished entering the coordinate just press the <Enter> key");
     System.out.println();
     System.out.println("* Pairs of point are used to create a lines");
     System.out.println("    - If the 2 points have mismatched dimensions, the point from the lower-dimension space is");
     System.out.println("      converted to a higher dimension point with added coordinate of 0");
     System.out.println("    - When a line is created, the line distance is provided");
     System.out.println();
     System.out.println("* When you are done specifying points and lines type \"EXIT\" to display final operation statistics");
     System.out.println("* All key words are case insensitive and can be abbreviated");
     System.out.println("* Random number will be scaled between -1,000.00 and +1,000.00");
     System.out.println();
     System.out.println("****************************** LAB 7 HELP MENU ******************************");
   }
   public static String userPrompt()
   {
     Scanner keyboard = new Scanner(System.in);
     System.out.println("Enter a coordinate: [R]andom: [H]elp: [E]xit to exit program: Enter key  for next point:");
     return keyboard.nextLine();
   }
   public static void mainDeal()
   {

     /** I pulled some code from this website:
      * http://www.java-forums.org/new-java/19024-detect-enter-key-being-pressed.html
      */
      while(true)
      {
      ans = userPrompt();

      if((ans == null)|| (ans.length() == 0) ||(ans.trim().equals("")))
      {
         System.out.println("Exiting...");
         break;
      }
      else if (ans.trim().toUpperCase().charAt(0) == 'R')
      {
        coord = Math.random();
        counter++;
        coordList.add(coord);

      }
      else if (ans.trim().toUpperCase().charAt(0) == 'E')
      {
        System.exit(0);
      }
      else if (ans.trim().toUpperCase().charAt(0) == 'H')
      {
        helpMenu();
      }
      else
      {
        coord = Double.parseDouble(ans);
        counter++;
        coordList.add(coord);
      }
    }
   }

}

最佳答案

Java 会自动将 Double 转换(拆箱)为 double,但它不会对数组执行相同的操作。您正在使用 Double[] 调用构造函数,并且它需要一个 double[]

关于java - 如果我尝试将数组作为参数传递到不同的公共(public)类中,为什么我的构造函数无法在 Java 中编译?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20157582/

相关文章:

java - 我的方法在什么时候应该停止抛出异常?

php - 如何使这个数组变平?

java - spring.kafka.consumer.auto-offset-reset 在 spring-kafka 中如何工作

java - 使用 spock 对 Spring Cloud Gateway 过滤器进行单元测试

javascript - 按人工顺序对 JavaScript 数组进行排序

arrays - 在给定的整数数组中找到一个以偶数频率出现的单个整数,而所有其他整数以奇数频率出现

class - 如何使用ExtJS 4.1.1以跨浏览器的方式获取元素类属性?

C++11 枚举类 : plus int failed to compile, 为什么?

c++ - 类对象数组的初始化 C++

java - 导入org.springframework.beans.Beanutils无法解析