gpt4 book ai didi

java - 从另一个类获取数组

转载 作者:太空宇宙 更新时间:2023-11-04 08:21:25 24 4
gpt4 key购买 nike

我正在写论文,需要帮助。我的论文的一部分是我需要创建一个游戏选择器,我的连接类型是蓝牙,客户端是Android手机,服务器是java。我使用 JFileChooser 来获取数组的值,我需要将该数组传递给另一个类。我尝试关注这篇文章http://www.dreaminco...ifferent-class/但在我的程序中,我似乎无法让它工作,这是我的代码:

DemoJFileChooser(我将获取路径[]数组的类)

package com.luugiathuy.apps.remotebluetooth;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class DemoJFileChooser extends JPanel implements ActionListener {
JButton go;

JFileChooser chooser;
String choosertitle;
public int x=0;
public String path[] = new String[10];

public DemoJFileChooser() {
go = new JButton("Do it");
go.addActionListener(this);
add(go);
}

public Dimension getPreferredSize(){
return new Dimension(200, 200);
}

public static void main(String s[]) {
Thread waitThread = new Thread(new WaitThread());
waitThread.start();

JFrame frame = new JFrame("");
DemoJFileChooser panel = new DemoJFileChooser();
frame.addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
);
frame.getContentPane().add(panel,"Center");
frame.setSize(panel.getPreferredSize());
frame.setVisible(true);
}

@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
int result;

chooser = new JFileChooser();
chooser.setCurrentDirectory(new java.io.File("."));
chooser.setDialogTitle(choosertitle);
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
//
// disable the "All files" option.
//
chooser.setAcceptAllFileFilterUsed(false);
//

if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
System.out.println("getCurrentDirectory(): "
+ chooser.getCurrentDirectory());
System.out.println("getSelectedFile() : "
+ chooser.getSelectedFile());

File paths = chooser.getSelectedFile();
path[x] = paths.getAbsoluteFile().toString();
x++;

for (int y=0;y<9;y++){
System.out.println(path[y]);
setArray(path);

}
}
else {
System.out.println("No Selection ");
}

}
void setArray(String paths[]) { // saves surname to variable
path= paths;

}

String[] getArray() { //gets the surname when called
return path;
}
}

ProcessConnectionThread(我需要数组的类)

package com.luugiathuy.apps.remotebluetooth;

import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.PointerInfo;
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.io.InputStream;
import java.io.OutputStream;
import com.luugiathuy.apps.remotebluetooth.DemoJFileChooser;


import javax.microedition.io.StreamConnection;

public class ProcessConnectionThread implements Runnable{
//DemoJFileChooser path;
//public String[] path = new String[10];

private StreamConnection mConnection;

// Constant that indicate command from devices
//private static final int EXIT_CMD = -1;
private static final int STEER_RIGHT = 1;
private static final int STEER_LEFT = 2;
private static final int ACCE = 3;
private static final int REV = 4;
private static final int MOVE = 5;
private static final int LEFT_CLICK = 6;
private static final int DOUBLE_CLICK = 7;
private static final int RIGHT_CLICK = 8;
private static final int HOLD = 9;
private static final int RELEASE = 10;
private static final int MOVEX = 11;
private static final int MOVEY = 12;
private static final int GAME = 20;
private static final int GAME1 = 11;
private static final int GAME2 = 12;
private static final int GAME3 = 13;
private static final int GAME4 = 14;
private static final int GAME5 = 15;
private DemoJFileChooser patharray[] = new DemoJFileChooser[10]; //create object array from student class

public ProcessConnectionThread(StreamConnection connection)
{
mConnection = connection;
}

public void FileChooser() { //constructor use keyword public just to make sure it is visible
for (int studI=0;studI<10;studI++) {
patharray[studI] = new DemoJFileChooser();
}
}
@Override
public void run() {
try {

// prepare to receive data
InputStream inputStream = mConnection.openInputStream();
OutputStream outputStream = mConnection.openOutputStream();
System.out.println("waiting for input");

while (true) {
int command = inputStream.read();
System.out.println(command);

processCommand(command);

}
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* Process the command from client
* @param command the command code
*/
private void processCommand(int command) {
try {


case GAME1:
for (int y=0;y<10;y++){
patharray[y].getArray();
System.out.println(patharray[y]);
}
Process p = Runtime.getRuntime().exec(patharray[0]);
p.waitFor();



break;


}
} catch (Exception e) {
e.printStackTrace();
}
}


}

我在 for 循环、patharray[] 上遇到错误。

任何帮助/建议将不胜感激。谢谢。

最佳答案

创建数组深拷贝的最简单方法是使用内置 Arrays.copyOf函数族。

例如:

String [] srcArray = { "item 1", "item 2", "item 3" };
String [] copy = Arrays.copyOf(srcArray, srcArray.length);

就您的代码而言,存在一些问题。在 DemoJFileChooser.actionPerformed您始终通过 System.out.println 打印路径数组的所有 10 个元素,但不能保证所有这些元素中都存在一个对象。您可能会在这里收到 NullPointerException。您的 for 循环应限制为 x 。另外,在该循环中您正在调用 setArray(path)这意味着您无缘无故地调用该函数 10 次。另外,你真的在​​做path = path因为你正在通过path进入setArray它分配给路径。

您还应该检查 x没有超过 path 的大小当您使用 path[x] = paths.getAbsoluteFile().toString(); 分配给它时.

作为一个单独的建议,而不是做 for(int i=0; i<10; i++) {...}每当您迭代数组时,您可能希望使用 for(int i=0; i<theArray.length; i++) {...}这样,如果您更改了数组大小,则无需更新整个程序中的 for 循环。

关于java - 从另一个类获取数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9472165/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com