Topic: 数组NEW默认值的问题

  Print this page

1.数组NEW默认值的问题 Copy to clipboard
Posted by: truthawp
Posted on: 2005-08-04 13:21

public class Test1
{
int arr[]=new int [10];
public static void main(String args[])
{
System.out.println(arr[1]);
}
}
<-- end tag is required.

书上说NEW创建INT数组时的默认值是0,但是上述程序为什么会出错呢?而且数组长度应该是0-9一共10,arr[1]应该不会益出的啊,错误提示非法??也不太明白

2.Re:数组NEW默认值的问题 [Re: truthawp] Copy to clipboard
Posted by: alexlauf
Posted on: 2005-08-04 15:10

System.out.println()参数类型必须是对象,而arr[1]的类型是基本类型INT。
应该使用对象包装器将基本类型转换为对象
改成System.out.println(new Integer(arr[1]))再试试

3.Re:数组NEW默认值的问题 [Re: truthawp] Copy to clipboard
Posted by: truthawp
Posted on: 2005-08-04 15:50

不好意思啊,转换为对象后还是无法通过编译,错误提示依旧,还是上面的几句 -_-!

4.Re:数组NEW默认值的问题 [Re: alexlauf] Copy to clipboard
Posted by: Reeves1016
Posted on: 2005-08-04 19:18

alexlauf wrote:
System.out.println()参数类型必须是对象,而arr[1]的类型是基本类型INT。
应该使用对象包装器将基本类型转换为对象
改成System.out.println(new Integer(arr[1]))再试试

谁所System.out.println()的参数必须是对象了,System.out.println(a int)照样可以,问题是搂住这个程序也太难看了

5.Re:数组NEW默认值的问题 [Re: truthawp] Copy to clipboard
Posted by: truthawp
Posted on: 2005-08-04 19:57

哪个~~~-_-! 请问"难看"是什么意思呢? 我想就短短几行,直接复制了,应该不影响阅读吧?如果System.out.println(a int)照样可以,那是不是程序里的命令没有错啊?那编译错是为什么呢?最近研究半个月来积累的问题,头快炸了,高手可怜可怜我这个菜鸟吧~

6.Re:数组NEW默认值的问题 [Re: truthawp] Copy to clipboard
Posted by: why
Posted on: 2005-08-04 20:36

Please try to use our [ code ] tag.

> 那编译错是为什么呢?最近研究半个月来积累的问题,头快炸了,高手可怜可怜我这个菜鸟吧~
It's more a problem about your ability in reading the compliation errors.

> Test1.java:10: illegal character
There could be unwanted characters at the end of certain line 10.
Don't ask me what it is, we can't tell.

After you have this corrected, then ...

public class Test1 {
  static int arr[] = new int[10]; //otherwise it can't be referenced in the static method main

  public static void main(String args[]) {
    System.out.println(arr[1]);
  }
}

7.Re:数组NEW默认值的问题 [Re: truthawp] Copy to clipboard
Posted by: truthawp
Posted on: 2005-08-04 21:32

恩,关于WHY前辈的意见,我一定虚心接受~
I tried what you said but it still failed~ -_-!

8.Re:数组NEW默认值的问题 [Re: truthawp] Copy to clipboard
Posted by: why
Posted on: 2005-08-04 22:05

Create a new file, type or copy-n-paste the above code, and try again.

9.Re:数组NEW默认值的问题 [Re: truthawp] Copy to clipboard
Posted by: truthawp
Posted on: 2005-08-04 22:12

Oh~!!!! It succeed~! Smile but,could you tell the reason? Smile
thank you all the same~
BTW,how to make my code look like yours? I tried "code" in the top but it didn't work~!

10.Re:数组NEW默认值的问题 [Re: truthawp] Copy to clipboard
Posted by: why
Posted on: 2005-08-04 22:51

truthawp wrote:
Oh~!!!! It succeed~! Smile but,could you tell the reason? Smile
thank you all the same~
BTW,how to make my code look like yours? I tried "code" in the top but it didn't work~!

end tag [ /code ] is required, check your first post.

As simply as the error message suggests -- there're illegal characters at the end of the line[s].
Perhaps it's a full-size semicolon -- check yourself.
Did you copy-n-paste the piece of code somewhere, say, a web site?

"Thank you all the same" What did you mean? In Chinese, please.

11.Re:数组NEW默认值的问题 [Re: truthawp] Copy to clipboard
Posted by: truthawp
Posted on: 2005-08-04 23:21

呵呵 完全明白了~ 代码是参照书上的,然后自己打上去的~
可能是我E文不太好吧,见笑了,但是仍然很感谢你(thank you all the same)Smile

12.Re:数组NEW默认值的问题 [Re: truthawp] Copy to clipboard
Posted by: yangun
Posted on: 2005-08-08 16:25

上面的问题主要是因为main方法中没有static variable arr让其引用。
只要将代码改为:
public class Test1
{
public static void main(String args[])
{
int arr[]=new int [10];
System.out.println(arr[1]);
}
}
即可。你不防试一试!

13.Re:数组NEW默认值的问题 [Re: truthawp] Copy to clipboard
Posted by: zhangp_happy
Posted on: 2005-08-08 20:47

你或者把数组定义在main()中也可以

14.Re:数组NEW默认值的问题 [Re: truthawp] Copy to clipboard
Posted by: zhangp_happy
Posted on: 2005-08-08 20:48

或者你加static在数组前

15.Re:数组NEW默认值的问题 [Re: truthawp] Copy to clipboard
Posted by: fossil
Posted on: 2005-08-28 09:36

why前辈是正解!int arr[]=new int [10];
光有这句是不行的!

16.Re:数组NEW默认值的问题 [Re: truthawp] Copy to clipboard
Posted by: luojun5715
Posted on: 2005-09-08 18:28

静态上下文不一致问题?

17.Re:数组NEW默认值的问题 [Re: truthawp] Copy to clipboard
Posted by: beingo
Posted on: 2005-09-09 10:26

静态方法是不能调用非静态的方法和变量的
要么把把数组定义成static中者把数组在main方法里
或者在main方法里生成test1的实例

18.这样写就OK了 [Re: truthawp] Copy to clipboard
Posted by: fq88
Posted on: 2005-09-20 00:16

public class Test1
{

  public static void main(String args[])
  { int arr[]=new int [10];
   System.out.println(arr[1]);
  }
}

19.Re:这样写就OK了 [Re: fq88] Copy to clipboard
Posted by: why
Posted on: 2005-09-20 00:46

fq88 wrote:
public class Test1
{

  public static void main(String args[])
  { int arr[]=new int [10];
   System.out.println(arr[1]);
  }
}

謝謝回應,不過樓上yangun壇友已指出相同的意見.
請以後盡量避免同樣情況,把大家發帖看帖的時間用在更有益的事上


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923