请教高手一个简单的java问题.......谢谢
//上机作业一,理解数组的应用;并调试正确//ArraySize.javaclassWeeble{};publicclassArraySize{publicstaticv...
//上机作业一,理解数组的应用;并调试正确
//ArraySize.java
class Weeble{};
public class ArraySize{
public static void main(){
//对象数组
weeble[] a;
weeble[]b = new weeble[5];
weeble[]c = new weeble[4];
for (int i=0;i<c.length;i++)
c[i] = new weeble();
weeble[] d= {
new Weeble(),new Weeble(),new Weeble()};
a = new Weeble[]{
new Weeble(); new Weeble()
};
System.out.println("a.length=" +a.length);
System.out.println("b.length=" +b.length);
for(int i=0; i<b.length;i++)
System.out.println("b["+i+"]=" +b[i]);
System.out.println("c.length=" +c.length);
System.out.println("d.length=" +d.length);
a = d;
System.out.println("a.length ="+a.length);
//基本类型的数组
int[]e;
int[] f = new int[5];
int[] g= new int[4];
for(int i=0;i<g.length;i++)
g[i]=i*i;
int[] h={11,47,93};
System.out.println("e.length ="+e.length);
System.out.println(f.length="+f.length);
for(int i=0;i<f.length;i++)
System.out.println("f["+i+"]="+f[i]);
System.out.println("g.length ="+g.length);
System.out.println("h.length ="+h.length);
e = h;
System.out.println("e.length ="+e.length);
e = new int[]{1,2};
System.out.println(e.length="+e.length);
}
}
这个程序调试的时候是错的.... 无法运行...请问如何改正呢?...谢谢 展开
//ArraySize.java
class Weeble{};
public class ArraySize{
public static void main(){
//对象数组
weeble[] a;
weeble[]b = new weeble[5];
weeble[]c = new weeble[4];
for (int i=0;i<c.length;i++)
c[i] = new weeble();
weeble[] d= {
new Weeble(),new Weeble(),new Weeble()};
a = new Weeble[]{
new Weeble(); new Weeble()
};
System.out.println("a.length=" +a.length);
System.out.println("b.length=" +b.length);
for(int i=0; i<b.length;i++)
System.out.println("b["+i+"]=" +b[i]);
System.out.println("c.length=" +c.length);
System.out.println("d.length=" +d.length);
a = d;
System.out.println("a.length ="+a.length);
//基本类型的数组
int[]e;
int[] f = new int[5];
int[] g= new int[4];
for(int i=0;i<g.length;i++)
g[i]=i*i;
int[] h={11,47,93};
System.out.println("e.length ="+e.length);
System.out.println(f.length="+f.length);
for(int i=0;i<f.length;i++)
System.out.println("f["+i+"]="+f[i]);
System.out.println("g.length ="+g.length);
System.out.println("h.length ="+h.length);
e = h;
System.out.println("e.length ="+e.length);
e = new int[]{1,2};
System.out.println(e.length="+e.length);
}
}
这个程序调试的时候是错的.... 无法运行...请问如何改正呢?...谢谢 展开
1个回答
展开全部
想问什么??
完成版
//ArraySize.java
class Weeble{};
public class ArraySize{
public static void main(String[] args){
//对象数组
Weeble[] a;
Weeble[] b = new Weeble[5];
Weeble[] c = new Weeble[4];
for (int i=0;i<c.length;i++)
c[i] = new Weeble();
Weeble[] d= {new Weeble(),new Weeble(),new Weeble()};
a = new Weeble[]{new Weeble(),new Weeble()};
System.out.println("a.length=" +a.length);
System.out.println("b.length=" +b.length);
for(int i=0; i<b.length;i++)
System.out.println("b["+i+"]=" +b[i]);
System.out.println("c.length=" +c.length);
System.out.println("d.length=" +d.length);
a = d;
System.out.println("a.length ="+a.length);
//基本类型的数组
int[] e = null;
int[] f = new int[5];
int[] g= new int[4];
for(int i=0;i<g.length;i++)
g[i]=i*i;
int[] h={11,47,93};
try
{
System.out.println("e.length ="+e.length);
System.out.println("f.length="+f.length);
for(int i=0;i<f.length;i++)
System.out.println("f["+i+"]="+f[i]);
System.out.println("g.length ="+g.length);
System.out.println("h.length ="+h.length);
e = h;
System.out.println("e.length ="+e.length);
e = new int[]{1,2};
System.out.println("e.length="+e.length);
}
catch(NullPointerException ex)
{
System.out.println("程序代码29行:\nSystem.out.println(\"e.length =\"+e.length);\n无法执行因为没有对int[] e 赋值");
}
}
}
完成版
//ArraySize.java
class Weeble{};
public class ArraySize{
public static void main(String[] args){
//对象数组
Weeble[] a;
Weeble[] b = new Weeble[5];
Weeble[] c = new Weeble[4];
for (int i=0;i<c.length;i++)
c[i] = new Weeble();
Weeble[] d= {new Weeble(),new Weeble(),new Weeble()};
a = new Weeble[]{new Weeble(),new Weeble()};
System.out.println("a.length=" +a.length);
System.out.println("b.length=" +b.length);
for(int i=0; i<b.length;i++)
System.out.println("b["+i+"]=" +b[i]);
System.out.println("c.length=" +c.length);
System.out.println("d.length=" +d.length);
a = d;
System.out.println("a.length ="+a.length);
//基本类型的数组
int[] e = null;
int[] f = new int[5];
int[] g= new int[4];
for(int i=0;i<g.length;i++)
g[i]=i*i;
int[] h={11,47,93};
try
{
System.out.println("e.length ="+e.length);
System.out.println("f.length="+f.length);
for(int i=0;i<f.length;i++)
System.out.println("f["+i+"]="+f[i]);
System.out.println("g.length ="+g.length);
System.out.println("h.length ="+h.length);
e = h;
System.out.println("e.length ="+e.length);
e = new int[]{1,2};
System.out.println("e.length="+e.length);
}
catch(NullPointerException ex)
{
System.out.println("程序代码29行:\nSystem.out.println(\"e.length =\"+e.length);\n无法执行因为没有对int[] e 赋值");
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询