Java 2 Dimensional Array构造函数

时间:2015-11-19 06:15:00

标签: java arrays multidimensional-array constructor

我需要为二维数组创建一个构造函数。

它应该是这样的:

public ClassName(double[][] input)

当我想稍后使用它时,我会输入ClassName num1 = new ClassName(????)。我已经被困在这几个小时了,不知道我应该放在括号内设置数组的大小。我已经尝试了一些有限的初学者知识,从尝试[5] [5]到5,5到5加倍[5] [5]到双[5] [5]输入,依此类推等等。

请帮忙......

谢谢。

4 个答案:

答案 0 :(得分:2)

这个例子更清楚地创建你自己的类。

#!/usr/bin/env python

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'taco_login.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui

try:
     _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
         return QtGui.QApplication.translate(context, text, disambig)

class Ui_login_window(QtGui.QDialog):
    def setupUi(self):
        self.setObjectName(_fromUtf8("login_window"))
        self.resize(255, 150)
        self.setMinimumSize(QtCore.QSize(255, 150))
        self.setMaximumSize(QtCore.QSize(255, 150))
        self.setStyleSheet(_fromUtf8("QDialog#login_window {\n"
"    background-color: qlineargradient(spread:pad, x1:0.559, y1:0, x2:0.555, y2:1, stop:0.445455 rgba(48, 48, 48, 255), stop:0.859091 rgba(61, 61, 61, 255), stop:1 rgba(79, 79, 79, 255));\n"
"}"))
        self.verticalLayout_2 = QtGui.QVBoxLayout(self)
        self.verticalLayout_2.setObjectName(_fromUtf8("verticalLayout_2"))
        self.widget = QtGui.QWidget(self)
        self.widget.setObjectName(_fromUtf8("widget"))
        self.password_lineEdit = QtGui.QLineEdit(self.widget)
        self.password_lineEdit.setGeometry(QtCore.QRect(10, 60, 221, 22))
        self.password_lineEdit.setStyleSheet(_fromUtf8("QWidget {border:none;}"))
        self.password_lineEdit.setInputMask(_fromUtf8(""))
        self.password_lineEdit.setEchoMode(QtGui.QLineEdit.Password)
        self.password_lineEdit.setObjectName(_fromUtf8("password_lineEdit"))
        self.login_btn = QtGui.QPushButton(self.widget)
        self.login_btn.setGeometry(QtCore.QRect(10, 100, 91, 17))
        palette = QtGui.QPalette()
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Button, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Base, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Window, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Button, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Base, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Window, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Button, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Base, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Window, brush)
        self.login_btn.setPalette(palette)
        self.login_btn.setAutoFillBackground(False)
        self.login_btn.setStyleSheet(_fromUtf8("QPushButton#login_btn {\n"
"    background-color: rgb(255, 255, 255);\n"
"    border:none;\n"
"}"))
        self.login_btn.setObjectName(_fromUtf8("login_btn"))
        self.username_lineEdit = QtGui.QLineEdit(self.widget)
        self.username_lineEdit.setGeometry(QtCore.QRect(10, 30, 221, 22))
        self.username_lineEdit.setStyleSheet(_fromUtf8("QWidget {border:none;}"))
        self.username_lineEdit.setInputMask(_fromUtf8(""))
        self.username_lineEdit.setObjectName(_fromUtf8("username_lineEdit"))
        self.text_1 = QtGui.QTextEdit(self.widget)
        self.text_1.setGeometry(QtCore.QRect(6, 0, 141, 25))
        self.text_1.setStyleSheet(_fromUtf8("QTextEdit#text_1{\n"
"border: none;\n"
"    background-color: transparent;\n"
"}\n"
""))
        self.text_1.setTextInteractionFlags(QtCore.Qt.NoTextInteraction)
        self.text_1.setObjectName(_fromUtf8("text_1"))
        self.cancel_btn = QtGui.QPushButton(self.widget)
        self.cancel_btn.setGeometry(QtCore.QRect(140, 100, 91, 17))
        palette = QtGui.QPalette()
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Button, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Base, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Window, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Button, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Base, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Window, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Button, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Base, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Window, brush)
        self.cancel_btn.setPalette(palette)
        self.cancel_btn.setAutoFillBackground(False)
        self.cancel_btn.setStyleSheet(_fromUtf8("QPushButton#cancel_btn {\n"
"    background-color: rgb(255, 255, 255);\n"
"    border:none;\n"
"}"))
        self.cancel_btn.setObjectName(_fromUtf8("cancel_btn"))
        self.verticalLayout_2.addWidget(self.widget)

        self.retranslateUi()
        QtCore.QMetaObject.connectSlotsByName(self)

    def retranslateUi(self):
        self.setWindowTitle(_translate("login_window", "TACO", None))
        self.password_lineEdit.setText(_translate("login_window", "Password", None))
        self.login_btn.setText(_translate("login_window", "Login", None))
        self.username_lineEdit.setText(_translate("login_window", "Username", None))
        self.text_1.setHtml(_translate("login_window", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'Sans\'; font-size:10pt; font-weight:400; font-style:normal;\">\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:9pt; font-weight:600; color:#f0f0f0;\">Welcome to TACO!</span></p></body></html>", None))
        self.cancel_btn.setText(_translate("login_window", "Cancel", None))

    @QtCore.pyqtSignature("on_cancel_btn_clicked()")
    def Cancel_btn(self):
        self.close()

if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    ui = Ui_login_window()
    ui.setupUi()
    ui.show()
    sys.exit(app.exec_())

在其他课程中,当您调用课程来执行此操作时

 public class ClassName { /* Attributes */ 
    private double[][] array;

    /* Constructor */
    public ClassName (double[][] array) {
    this.array = array;
   }
}

答案 1 :(得分:1)

当数组作为匿名数组传入时,您需要实例化数组(这意味着您还没有包含数组的变量)。您没有提供您尝试过的代码,但我的假设是您遗漏了new关键字。

ClassName num1 = new ClassName(new double[x][y]);

答案 2 :(得分:1)

您可以先声明数组,然后使用new运算符为它分配内存。

double input[][];
input = new double[size][size];

//simply use 
double input[][] = new double[size][size];

//for class use
YourClass instance = new YourCLass(input); 

答案 3 :(得分:0)

您可能需要以下内容:

{{1}}