Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (480)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (547)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Problem with arrays.. o.O  (Read 2256 times)
0 Members and 1 Guest are viewing this topic.
Offline kidluff

Senior Newbie





« Posted 2006-06-02 07:25:39 »

How come this will work:
1  
int i={1,2,3};


..but this will not  Huh:
1  
2  
int i;
i={1,2,3};


How can I make the second one work O_o?
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 781
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #1 - Posted 2006-06-02 07:44:57 »

The first line also doesn't work (at least it doesn't compile in Eclipse and I'd doubt such a bug would be in javac)

it is not an int, it is an int-array.

There are multiple ways to define it:
int[] a = {0, 1, 2};
int[] a = new int[]{0, 1, 2};
int[] a = new int[3]; a[0] = 0; a[1] = 1; a[2] = 2;

And you can replace the "int[] a" by "int a[]" for some obscure reason.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline kidluff

Senior Newbie





« Reply #2 - Posted 2006-06-02 19:41:11 »

Oops, I meant to put:
1  
int i[]={1,2,3};

1  
2  
int i[];
i={1,2,3};


but yeah you answered my question, thanks  Grin
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline oNyx

JGO Coder


Medals: 2


pixels! :x


« Reply #3 - Posted 2006-06-02 21:05:25 »

>And you can replace the "int[] a" by "int a[]" for some obscure reason.

You can also write int []a or even int[]a. What doesnt work is []int a Wink

弾幕 ☆ @mahonnaiseblog
Offline woogley
« Reply #4 - Posted 2006-06-02 21:20:48 »

at instantiation, you can use just braces, like so...
1  
int x[] = {1,2,3};


however, if you're trying to intialize the variable after it has been declared, plain ol' braces won't do the trick:

1  
2  
int x[];
x = {1,2,3}; // wont compile


you have to re-specify the type..

1  
2  
int x[];
x = new int[] {1,2,3};


who knows why. *shrug*
Offline Mr_Light

Senior Member




shiny.


« Reply #5 - Posted 2006-06-03 16:32:11 »

I'm gonna gues, it might already be assigned and it would be unclear how to interpert it?

It's harder to read code than to write it. - it's even harder to write readable code.

The gospel of brother Riven: "The guarantee that all bugs are in *your* code is worth gold." Amen brother a-m-e-n.
Offline Anon666

Junior Member




aka Abuse/AbU5e/TehJumpingJawa


« Reply #6 - Posted 2006-06-03 21:19:00 »

The shorthand notation is ambiguous in certain circumstances, so cannot be used all the time.
Therefor, for consistency I never use it.

Also, interestingly enough - I just found an erroneous compilation error message given by the Eclipse compiler =)

1  
Object [] c = {{}};


Gives the compiler error :-

Quote
Type mismatch: cannot convert from Object[] to Object

Quite clearly Object[], being an Object itself should *always* be able to be converted to Object.

Sun's javac is alot more clear in its error :-

Quote
illegal initializer for java.lang.Object
Offline Jeff

JGO Coder




Got any cats?


« Reply #7 - Posted 2006-06-04 02:45:10 »



And you can replace the "int[] a" by "int a[]" for some obscure reason.

The obscure reason is that int[] a makes a lot more sense, but int a[] is how C  does it and the language desigenrs were afraid of the programmer lashback if they didnt make it work that way, too.

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Offline woogley
« Reply #8 - Posted 2006-06-04 02:59:05 »

programmer lashback such as lack of operator overloading.. etc Roll Eyes
Offline oNyx

JGO Coder


Medals: 2


pixels! :x


« Reply #9 - Posted 2006-06-04 06:46:54 »

programmer lashback such as lack of operator overloading.. etc Roll Eyes

Well, if you ask programmers what they would do with operator overloading they only mention vector stuff, which like... makes some sense. The other useful thing you can do with operator overloading is unnecessary in java, because its managed to begin with.

Other than that operator overloading causes more trouble than it helps.

So... dunno... guess most would be happy if there were useful default vector classes with overloaded operators (like String). Is there any other default thing? (Apart from String, which is already there... vectors, which I just mentioned... and trying to make it sorta smart, which isnt neccessary.)

弾幕 ☆ @mahonnaiseblog
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Jeff

JGO Coder




Got any cats?


« Reply #10 - Posted 2006-06-04 08:20:44 »

programmer lashback such as lack of operator overloading.. etc Roll Eyes

You will find programmer opiniosn evry mixed on the alck of operator overlaoding.

I for one agreewith the deicsion not to include it as I feel the obfuscatory nature of it vastly outweighs its usefulness.

YMMV.

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Offline woogley
« Reply #11 - Posted 2006-06-04 12:21:30 »

lucky for me, java was my first language, and you can't miss what you never had  Wink

back to the original topic, I kinda wish C# allowed both type of array declaration like java does. I usually use the C-style declaration so I can keep my same-type declarations on one line, like...

1  
int x,y,speed[]; // one line


but in C# you have to use a brand new line..

1  
2  
int x,y;
int[] speed;


small-scale example, but it just annoys me. *shrug*
Offline Anon666

Junior Member




aka Abuse/AbU5e/TehJumpingJawa


« Reply #12 - Posted 2006-06-04 14:19:52 »

I would consider the first example bad code  Grin

My reasoning is 2 fold :-
1)
Quote
so I can keep my same-type declarations on one line

As im sure you are aware this is false;
an int[] is an Object type, an int is a primitive type.

2) I only ever place variables declarations that *are* the type on a single line, if the value of one is meaningless without knowing the values of the others. (as your example demonstrates, x & y are the most obvious example)
Offline woogley
« Reply #13 - Posted 2006-06-04 14:24:10 »

yeah int[] may be object, but I don't think of it that way Wink

I look at it as "a lot of ints" and therefore they stay on my int line! >=P
Offline Mr_Light

Senior Member




shiny.


« Reply #14 - Posted 2006-06-04 14:33:12 »

most places I go coding conventions define that you should declare every field on a new line, reguarding methodes, some best practice defined that scopes of var should be as small as possible.

It's harder to read code than to write it. - it's even harder to write readable code.

The gospel of brother Riven: "The guarantee that all bugs are in *your* code is worth gold." Amen brother a-m-e-n.
Offline woogley
« Reply #15 - Posted 2006-06-05 05:17:58 »

it's a good thing none of this really affects the outcome at runtime, since we all go by different preferences
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

atombrot (23 views)
2014-08-19 09:29:53

Tekkerue (22 views)
2014-08-16 06:45:27

Tekkerue (21 views)
2014-08-16 06:22:17

Tekkerue (12 views)
2014-08-16 06:20:21

Tekkerue (19 views)
2014-08-16 06:12:11

Rayexar (57 views)
2014-08-11 02:49:23

BurntPizza (37 views)
2014-08-09 21:09:32

BurntPizza (29 views)
2014-08-08 02:01:56

Norakomi (36 views)
2014-08-06 19:49:38

BurntPizza (66 views)
2014-08-03 02:57:17
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!