Ассоциативные массивы
Link to ассоциативные-массивы
An Associative Array (sometimes also called a Map or a Dictionary) is like a normal Array in the way that it is able to store multiple entries. Unlike Arrays however, you can choose what type you want the index, or (as we call it in maps) key, to be!
Объявление ассоциативного массива
Link to объявление-ассоциативного-массива
Ассоциативные массивы объявляются с помощью фигурных скобок{}
и двоеточий :
.
ZenScript Copyval myAssocArray = {
dirt : <minecraft:dirt>,
gold : <minecraft:gold_ingot>
} as IItemStack[string];
Давайте разберемся с этим, да?
val myAssocArray =
— стандартное объявление переменной,{
— ассоциативный массив, сэр!dirt : <minecraft:dirt>
— мы присваиваем значение<minecraft:dirt>
к ключуdirt
,,
wait, there's more to comegold : <minecraft:gold_ingot>
we map<minecraft:gold_ingot>
under the stringgold
}
we have reached the end of the Array, Sir!as IItemStack[string];
this is an Associative Array that uses strings as indices and IItemStacks as values.
Окей, что мне нужно подумать насчет использования всего этого?
- You can use about every type available to Zenscript as either key or value.
- You cannot use variables for key declaration in the initial Declaration (the one that uses
{}
) as clear Text is interpreted as string!
Обращение к элементам ассоциативного массива
Link to обращение-к-элементам-ассоциативного-массива
You refer to items inside an Associative Array the same way you refer to items inside a normal Array:
Array[index]
Only difference is this time, you don't necessarily need to use an Integer as index, but whatever type you declared your Array to be!
ZenScript Copy
val dirt = <minecraft:dirt>;
val assocArray = {
<minecraft:dirt> : "This is me"
} as string[IItemStack];
//array[index]
print(assocArray[<minecraft:dirt>]);
//You can also use variables here, as long as the variable is of the correct type
print(assocArray[dirt]);
There is one special case, that is when you use strings as indeces:
In this case you can also use the memberGetter like this:
ZenScript Copyval assocWithStrings = {
//you can use "" if you want
"one" : "1",
//but you don't have to
two : "2"
} as string[string];
//You can either use the memberGetter
print(assocWithStrings.one);
//Or the standard index Getter
print(assocWithStrings["two"]);
Манипулирование элементами внутри ассоциативного массива
Link to манипулирование-элементами-внутри-ассоциативного-массива
As in Arrays, you can manipulate items inside an Associative Array using array[index] = newValue
.
There is one major differenc though:
While Arrays have a fixed size, maps don't. That means you can always add an entry by setting to an index that has previously not been set!
ZenScript Copyval changingArray = {
<minecraft:dirt> : "this is me",
<minecraft:gold_ingot> : "and I hate it"
} as string[IItemStack];
val gg = <minecraft:gold>;
//Overrides the value of gold_ingot
changingArray[gg] = "and I love it";
//adds a new entry
changingArray[<minecraft:grass>] = "Power!";
Retrieving an Associative Array's Key and Entrysets
Link to retrieving-an-associative-arrays-key-and-entrysets
The KeySet is an array containing all the map's keys.
The valueSet is an array containing all the map's values.
The entrySet is an array containing all the map's entries (see below).
ZenScript CopymyAssocArray.keySet //keySet
myAssocArray.keys //keySet
myAssocArray.values //valueSet
myAssocArray.valueSet //valueSet
myAssocArray.entrySet //entrySet
Перебор ассоциативного массива
Link to перебор-ассоциативного-массива
There are two Iterators that allow you to iterate over an Associative Array:
- The key-Iterator: Iterates over the keys, uses one variable
- The key-value-Iterator: Iterates over the keys and values, uses two variables
Let's add an Associative Array that stores crafting recipes to be iterated over:
- Keys shall be the crafting output as IItemStack
- Values shall be the crafting ingredients as IIngredient
- We shall use the key-Iterator that is built like this:
for key in assocArray {doSth;}
- We shall also use the key-value-Iterator that is built like this
for key, value in assocArray {doSth;}
ZenScript Copyimport crafttweaker.item.IItemStack;
import crafttweaker.item.IIngredient;
val dirt = <minecraft:dirt>;
val recipeMapShaped = {
<minecraft:grass> : [[dirt, dirt, dirt],[dirt, dirt, dirt],[dirt, dirt, dirt]],
<minecraft:gold_ingot> : [[dirt, dirt, dirt],[dirt, <minecraft:gold_ingot>, dirt],[dirt, dirt, dirt]]
} as IIngredient[][][IItemStack];
recipeMapShaped[dirt] = [[dirt, dirt, dirt],[dirt, null, dirt],[dirt, dirt, dirt]];
//key will be grass, goldIngot, dirt
for key in recipeMapShaped {
recipes.addShaped(key, recipeMapShaped[key]);
}
//keys will be grass, goldIngot, dirt, values will be the recipes for them
for key, value in recipeMapShaped {
recipes.addShaped(key, value);
}
ZenType Entry
Link to zentype-entry
A map Entry consists of a key and a value.
Currently the only way to get such an object is via a map's entrySet method.
You can use the getters to get key
and value
ZenScript Copy//Replace map with a reference to an existing map/associative array
val myEntry = map.entrySet[0];
myEntry.key; //Returns the entry's key.
myEntry.value; //Returns the entry's value.