简体   繁体   中英

Java: how do I point at an array item through a variable?

How do I point at an array item through a variable? Example:

String[] names;

int test = 10;

String myName = names[test];

The purpose is to get the same number item from 2 different arrays, for a username/password program.

In your example:

String[] names;
int test = 10;
String myName = names[test];

The variable myName is a reference to the String in the array, but giving it new value:

myName = "fred";

will do nothing to the array - all you'd be doing is assigning a new reference to that variable.

To give the array element a bew value, do this:

names[test] = "fred";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM