如果我尝试在不同的公共类中将数组作为参数传递,为啥我的构造函数无法在 Java 中编译?
Posted
技术标签:
【中文标题】如果我尝试在不同的公共类中将数组作为参数传递,为啥我的构造函数无法在 Java 中编译?【英文标题】:How come my constructor will not compile in Java if I try and pass an array as an argument in a different public class?如果我尝试在不同的公共类中将数组作为参数传递,为什么我的构造函数无法在 Java 中编译? 【发布时间】:2013-12-08 02:01:16 【问题描述】:我正在尝试编译此代码,但不断收到错误消息- 错误:/Users/Pedro/Dropbox/school/java/Proj/exam 3/EchoText.java:23:找不到符号 符号:构造函数 MultiPoint(java.lang.Double[]) 位置:多点类
如果我在公共 EchoText 的构造函数中省略该数组,它将编译。但是,我需要将数组作为参数传递给 MultiPoint 公共类。我的问题是为什么它不能与参数一起编译,这个错误到底是什么意思?
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);
【问题讨论】:
【参考方案1】:MultiPoint
构造函数接受double[]
类型,如下所示:
public MultiPoint(double[] coordPt)
但是,当您创建 MultiPoint
的新实例时,您将传递一个 Double[]
类型,如下所示:
Double coordPt[] = new Double[ coordList.size() ];
MultiPoint myMultiPoint = new MultiPoint(coordPt);
【讨论】:
【参考方案2】:Java 会自动为您将Double
转换(拆箱)为double
,但它不会为数组做同样的事情。你用Double[]
调用你的构造函数,它期待double[]
。
【讨论】:
我将所有与数组相关的代码从 double[] 转换为 Double[] 并编译。谢谢。以上是关于如果我尝试在不同的公共类中将数组作为参数传递,为啥我的构造函数无法在 Java 中编译?的主要内容,如果未能解决你的问题,请参考以下文章