I'm new to java, but have done some programming in other languages before. I have an array that I declared. This one is for the basic equipment, and each of the basicSimple* have been correctly declared earlier on:
//Creating template...
Item[] basicSet = new Item[15];
basicSet [0] = basicSimpleShoes;
basicSet [2] = basicSimplePants;
basicSet [4] = basicSimpleShirt;
basicSet [6] = basicSimpleGloves;
I'm trying to copy the array to some other ones, each for one of the possible formats, one of which looks like this:
//Strongarm set...
Item[] strongarmBeginnerSet = new Item[15];
strongarmBeginnerSet [0,6] = basicSet[0,6];
strongarmBeginnerSet [9] = basicSimpleShortsword;
strongarmBeginnerSet [10] = basicSimpleShield;
Again, the basicSimple* Items are correctly declared earlier on. I keep on getting an error, something about a missing closing bracket, but I can't find where it is. Do I have to declare each piece individually or is there a function I'm unaware of for this scenario?
This simply won't work in Java, the slice notation exists for other programming languages (say, Python), but not in Java:
strongarmBeginnerSet[0,6] = basicSet[0,6]; // compiler reports a syntax error!
You have to copy each element in turn, like this (and I'm assuming that you intended to copy up to and including the seventh element located at index 6
):
for (int i = 0; i < 7; i++)
strongarmBeginnerSet[i] = basicSet[i];
Or alternatively, use System.arraycopy()
:
System.arraycopy(basicSet, 0, strongarmBeginnerSet, 0, 7);
strongarmBeginnerSet [0,6] = basicSet[0,6];
is not valid syntax in Java. You will need to use a for
loop instead.
strongarmBeginnerSet [0,6] = basicSet[0,6];
is invalid syntax for Java.
You could use System.arraycopy
to achieve the same result...
Something like...
System.arraycopy(basicSet, 0, strongarmBeginnerSet, 0, 6);
For example...
要复制数组的一部分,请使用:
strongarmBeginnerSet [6] = Arrays.asList(basicSet).subList(0,7).toArray();
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.