由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Java版 - an Array question
相关主题
Array 能不能用ArrayList作为它的元素?Question on JSP EL
How would clear a BufferedImage?[合集] Web Service Java 客户端的问题
HashMap 怎样循环用更快?[合集] How to check whether a file is locked?
structure in Java??[合集] How to get all classes under a package?
String[] a = c.toArray(new String[0])[合集] java 得 inner class 有没有 static 有什么区别啊?
[转载] javascript怎么访问JSP里的arraylist?[合集] Is there any free java PDF viewer?
static final的问题[合集] 问一下这个cast在java里是怎么work的
[转载] Re: java thread problemHow do I declare a transaction among 2 spring service calls
相关话题的讨论汇总
话题: myarray话题: int话题: array话题: new话题: do
进入Java版参与讨论
1 (共1页)
P****y
发帖数: 707
1
Why can't I do the following:
int [] myArray;
myArray = {1, 2};
Thanks
o**v
发帖数: 1662
2
int[] myArray = new int[2];
myArray[0]=1
myArray[1]=2

【在 P****y 的大作中提到】
: Why can't I do the following:
: int [] myArray;
: myArray = {1, 2};
: Thanks

P****y
发帖数: 707
3
thank you, ooev. I just do not understand why it is illegal to
do it on my way.

【在 o**v 的大作中提到】
: int[] myArray = new int[2];
: myArray[0]=1
: myArray[1]=2

o**v
发帖数: 1662
4
or
int[] myArray = {1, 2};

【在 P****y 的大作中提到】
: Why can't I do the following:
: int [] myArray;
: myArray = {1, 2};
: Thanks

c*y
发帖数: 137
5
You need to specify how much memory to allocate to the array when defining it.

【在 P****y 的大作中提到】
: thank you, ooev. I just do not understand why it is illegal to
: do it on my way.

m******t
发帖数: 2416
6
I'm too lazy to test it now, but I think you want to do:
int[] myArray = new int[]{1, 2};

【在 o**v 的大作中提到】
: or
: int[] myArray = {1, 2};

o**v
发帖数: 1662
7
i tested b4 i posted. 【 在 magicfat (魔法胖子) 的大作中提到: 】
m******t
发帖数: 2416
8
you are right, looks like if you want to assign it in the declaration
you can do either
int[] myArray = new int[]{1, 2};
or simply
int[] myArray = {1, 2};
But if it's in an assignment, you can't omit "new int[]".

【在 o**v 的大作中提到】
: i tested b4 i posted. 【 在 magicfat (魔法胖子) 的大作中提到: 】
c**g
发帖数: 274
9
Java array is an object, to create an object, you need to
1) declare
2) initialize (use new to assign a size).
So you can say:
int[] myArray;
myArray = new int[4];
But one of many special things about an array is, you can also
initialize its members, either in an explicitive (such as a loop),
or an implicitvie way. like:
int[] myArray;
myArray = new int[] {1, 2};
you can do this in one line: int[] myArray = new int[] {1, 2};
ALSO, there is a shortcut for the above one line initi

【在 P****y 的大作中提到】
: Why can't I do the following:
: int [] myArray;
: myArray = {1, 2};
: Thanks

1 (共1页)
进入Java版参与讨论
相关主题
How do I declare a transaction among 2 spring service callsString[] a = c.toArray(new String[0])
问个可能比较奇怪的弱问题[转载] javascript怎么访问JSP里的arraylist?
https certificate??static final的问题
请教:windows xp下面可以装cvsnt么?[转载] Re: java thread problem
Array 能不能用ArrayList作为它的元素?Question on JSP EL
How would clear a BufferedImage?[合集] Web Service Java 客户端的问题
HashMap 怎样循环用更快?[合集] How to check whether a file is locked?
structure in Java??[合集] How to get all classes under a package?
相关话题的讨论汇总
话题: myarray话题: int话题: array话题: new话题: do