We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › char as Index, Index starting with 1 - possible
Page Index Toggle Pages: 1
char as Index, Index starting with 1 - possible? (Read 760 times)
char as Index, Index starting with 1 - possible?
Mar 27th, 2010, 3:07am
 
Hello all,

can I have

 String[][] Buffer = new String[8][8];

and tell it to go only from

String['A' to 'H'][ 1 to 8 ];
?

This is asking:
Can I use a char or string as index?
And can I start elsewhere than 0 (here 1)
and can I mix char and int.

Or is is bad style?

Aiming at a game of chess.

Thank you!

Chrisir

Re: char as Index, Index starting with 1 - possible?
Reply #1 - Mar 27th, 2010, 10:19am
 
"Can I use a char or string as index? "
No. (But there is HashMap, not useful in your case.)

"And can I start elsewhere than 0 (here 1) "
No, but you can declare an array of 9 elements and just ignore the first.

"can I mix char and int"
Not sure what you mean by that, but for information a char is a number in Java.

"Aiming at a game of chess."
You can map 'A' to 1, 'B' to 2, eg. with something like:
int pos = letter - 'A' + 1; // letter is a char
Re: char as Index, Index starting with 1 - possible?
Reply #2 - Mar 27th, 2010, 10:33am
 
Somehow, I knew it would be you how anwers that...   Wink

You are just somebody who likes ambitious tiny questions...  Wink

Thank you!

Re: char as Index, Index starting with 1 - possible?
Reply #3 - Mar 27th, 2010, 2:29pm
 

I posted the small chess-thing.

The pciking is only reliable with OPENGL.

http://www.openprocessing.org/visuals/?visualID=8530

Chrisir    Wink  

Re: char as Index, Index starting with 1 - possible?
Reply #4 - Mar 27th, 2010, 5:22pm
 
Chrisir wrote on Mar 27th, 2010, 3:07am:
Hello all,
String['A' to 'H'][ 1 to 8 ];


You can't use a string to index an array; and you can use a char (which is a number) and translate char 'A' to zero by "letter - 'A' + 1" trick; however, better yet, you can declare some constants:

// or use char to save a tad bit of space
final int A = 0, B = 1, C = 2, D = 3, E = 4, F = 5, G = 6, H = 7;

and access your array like:
a = Buffer[A][1];
Page Index Toggle Pages: 1