Java中同一个类中一个函数如何调用另一个函数中的数组?

代码如下,同一个类下,在btnNewButton.addActionListener()中定义了数组array[],现在想要在btnNewButton_1.addActi... 代码如下,同一个类下,在btnNewButton.addActionListener()中定义了数组array[],现在想要在btnNewButton_1.addActionListener()调用这个数组,应该怎么实现?添加什么代码?

JButton btnNewButton = new JButton("\u751F\u6210\u968F\u673A\u6570");
btnNewButton.addActionListener(new ActionListener() {
public int[] array=new int[10];
public void actionPerformed(ActionEvent e) {
Random random=new Random();
textArea.setText("");
for(int i=0;i<array.length;i++){
array[i]=random.nextInt(50);
textArea.append(array[i]+" ");
}
}
});
btnNewButton.setBounds(171, 103, 95, 25);
contentPane.add(btnNewButton);

JButton btnNewButton_1 = new JButton("\u9009\u62E9\u6392\u5E8F\u6CD5");
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textArea_1.setText("");
int index;

for(int i=1;i<array.length;i++){
index=0;
for(int j=1;j<=array.length-i;j++){
if(array[j]>array[index]){
index=j;
}
}
int temp=array[array.length-i];
array[array.length-i]=array[index];
array[index]=temp;
}
for(int i=0;i<array.length;i++){
textArea_1.append(array[i]+" ");
}
}
});
btnNewButton_1.setBounds(171, 226, 95, 25);
contentPane.add(btnNewButton_1);
展开
 我来答
雪飞潇潇
2015-11-03 · TA获得超过6275个赞
知道大有可为答主
回答量:1968
采纳率:91%
帮助的人:918万
展开全部

把int[] array 定义为全局变量

public class Test{
    public static int[] arry;
    
}
本回答被提问者和网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式