import javax.swing.*;public class SwapTest { public static void main(String args[]) { int a[]=new int[1],b[]=new int[1]; String inputA,inputB; inputA=JOptionPane.showInputDialog("Enter first integer"); a[1]=Integer.parseInt(inputA); inputB=JOptionPane.showInputDialog("Enter second integer"); b[1]=Integer.parseInt(inputB); swap(a,b); JOptionPane.showMessageDialog(null,"a is: "+a+" b is: "+b,"The result of sawp a and b",JOptionPane.INFORMATION_MESSAGE); System.exit(0); }
public static void swap(int[] i,int[]j) { int temp[]=new int[1]; i=new int[1]; j=new int[1]; temp=i; i=j; j=temp; } } 运行输入第一个数就出错: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1 at SwapTest.main(SwapTest.java:10) 怎么会数组越界呢,帮忙看看小弟的代码哪出了问题,语法,格式,习惯,风格上的都行 我要交换a,b该怎么写swap()好呢. 先谢谢各位了
inputA=JOptionPane.showInputDialog("Enter first integer"); a[1]=Integer.parseInt(inputA); inputB=JOptionPane.showInputDialog("Enter second integer"); b[1]=Integer.parseInt(inputB); 因该是这样吧!! inputA=JOptionPane.showInputDialog("Enter first integer"); a[0]=Integer.parseInt(inputA); inputB=JOptionPane.showInputDialog("Enter second integer"); b[0]=Integer.parseInt(inputB);
import javax.swing.*;public class SwapTest { public static void main(String args[]) { int a[]=new int[1], b[]=new int[1]; String inputA,inputB; inputA=JOptionPane.showInputDialog("Enter first integer"); a[0]=Integer.parseInt(inputA); inputB=JOptionPane.showInputDialog("Enter second integer"); b[0]=Integer.parseInt(inputB); swap(a,b); // 下一句要改成:访问数组的值 JOptionPane.showMessageDialog(null,"a is: "+a[0]+" b is: "+b[0],"The result of sawp a and b",JOptionPane.INFORMATION_MESSAGE); System.exit(0); }
USERPROFILE=F:\
这样就可以解决第二个问题。
但是不建议这样做。
{
public static void main(String args[])
{
int a[]=new int[1],b[]=new int[1];
String inputA,inputB;
inputA=JOptionPane.showInputDialog("Enter first integer");
a[1]=Integer.parseInt(inputA);
inputB=JOptionPane.showInputDialog("Enter second integer");
b[1]=Integer.parseInt(inputB);
swap(a,b);
JOptionPane.showMessageDialog(null,"a is: "+a+" b is: "+b,"The result of sawp a and b",JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
public static void swap(int[] i,int[]j)
{
int temp[]=new int[1];
i=new int[1];
j=new int[1];
temp=i;
i=j;
j=temp;
}
} 运行输入第一个数就出错:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1 at SwapTest.main(SwapTest.java:10) 怎么会数组越界呢,帮忙看看小弟的代码哪出了问题,语法,格式,习惯,风格上的都行
我要交换a,b该怎么写swap()好呢.
先谢谢各位了
a[1]=Integer.parseInt(inputA);
inputB=JOptionPane.showInputDialog("Enter second integer");
b[1]=Integer.parseInt(inputB);
因该是这样吧!!
inputA=JOptionPane.showInputDialog("Enter first integer");
a[0]=Integer.parseInt(inputA);
inputB=JOptionPane.showInputDialog("Enter second integer");
b[0]=Integer.parseInt(inputB);
{
public static void main(String args[])
{
int a[]=new int[1], b[]=new int[1];
String inputA,inputB;
inputA=JOptionPane.showInputDialog("Enter first integer");
a[0]=Integer.parseInt(inputA);
inputB=JOptionPane.showInputDialog("Enter second integer");
b[0]=Integer.parseInt(inputB);
swap(a,b);
// 下一句要改成:访问数组的值
JOptionPane.showMessageDialog(null,"a is: "+a[0]+" b is: "+b[0],"The result of sawp a and b",JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
public static void swap(int[] i,int[]j)
{
int temp;
//int temp[]=new int[1];不要change整个数组
//i=new int[1];这样做是不行的,在函数括号外会被回收
//j=new int[1];
temp=i[0];
i[0]=j[0];
j[0]=temp;
}
}
编译后运行试试!!!^L^