How to get mutableList of mutableList in kotlin? - arrays

I am trying to get mutableList of mutableList of LatLng pairs in kotlin. Below is my code.
When the function onPauseButtonClicked()is called I get the value in locationlist variable which is mutableList of LatLng pairs.But I am not able to add the value to locationlists variable which is a mutableListOf(locationList) . The value is shown empty. What is wrong with code.
private var locationList = mutableListOf<LatLng>()
private var locationlists = mutableListOf(locationList)
/**
This is function where locationlists is called
*/
private fun onPauseButtonClicked(){
locationlists.add(locationList)
// Toast.makeText(requireContext() ,"$locationList", Toast.LENGTH_SHORT).show()
val c= locationlists[0]
Toast.makeText(requireContext() ,"$c", Toast.LENGTH_SHORT).show()
}

add will add the MutableList to the end of the list. However, when you call mutableListOf(locationList) you already put an empty MutableList as the first element. It's easy to verify in the debugger, that your locationlists will have two elements, eventhough you only call add once.
Therefore, the correct way would be to initialize locationlists with an empty list of MutableList
var locationlists = mutableListOf<MutableList<LatLng>>()

While initializing use
private val locationLists = MutableList<MutableList<LatLang>>(0){mutableListOf()}

Related

SWIFT: assigning all values of an item in a structure array to a variable

Sorry, I don't even have an idea of the keywords to search for answer.
I want to store all items within a global structure to a local variable.
struct HighScores: Codable {
var highscoreRecord: [HighscoreRecord]
}
struct HighscoreRecord: Codable {
var Rank:Int
var Date:Date
var avDuration:Float
var Score:Int
}
A global variable is based on this structure and populated within a UIViewController
var jsonResult: HighScores?
Now, in another UIViewController, I want to extract the values of Score for all Highscores and store it to a local variable. I thought it should look somewhat like this, however, I do not get it to work
#IBDesignable class ScoreTimeGraphView: UIView {
var graphScore = jsonResult!.highscoreRecord.Score
The declaration above throws "Value of type '[HighscoreRecord]' has no member 'Score'"
Any ideas how to do this?
Cheers!
highscoreRecord is an Array. You can't use .Score directly on it because the Array type doesn't have a property named Score.
However, because it's element type is HighScore (which does have the property you want), you can iterate over it and collect the Score property from each one.
I think this is what you are after:
var allGraphScores = jsonResult!.highscoreRecord.map { $0.Score }
.map(_:) takes a closure with one parameter, and passes in each element of a sequence in turn.
So, highscoreRecord.map { $0.Score } returns a new array, by finding the Score property of each HighScoreRecord in the array highscoreRecord.
PS it's probably a good idea to name your variables using lowercase camelCase, for readability and instant recognition by any Swift dev that Score is a variable and not an object.
You're trying to access the property from an array. You need to provide an index to remove the error. Update this line:
var graphScore = jsonResult!.highscoreRecord.Score
To this:
var graphScore = jsonResult!.highscoreRecord[0].Score

How do I encode an array of Byte32 values for Web3j to pass to my smart contract?

Contract function is defined as:
function createAggregate (string memory key, bytes32[2] memory part_array) public returns (bytes32)
and have incoming a list of parts, defined as...
List<Bytes32> elements
so was trying to use:
List<Type> items = new ArrayList<Type>();
items.add(...); // user reference
items.add(new DynamicArray<>(elements));
final Function function = new Function("createAggregate",
items,
Arrays.asList(new TypeReference<Bytes32>() {})
);
...
But this does not work, seems to be an encoding issue - what is the right what of encoding the Bytes32 ? (This seems to work fine for an array of strings)
Sort of solved this with the following (although was really looking for a more dynamic size solution)
new StaticArray2(Bytes32.class, Utils.typeMap(elements, Bytes32.class));

How to use Array in JEXL?

Using JEXL, I am trying to initialize array and than adding elements into it, however below code gives me 'unsolvable property '0' error.
var abc=[];
abc[0]=5;
1) How can I initialize empty array and keep adding values in it?
2) Can I use it like List, where I do not need to specify size at the time of initialization ?
in JEXL syntax you can initialize objects with new function.
Other option is to add to context arraylist:
This is a working example with jexl2:
JexlEngine jexl = new JexlEngine();
String jexlExp = "var abc=new(\"java.util.ArrayList\", 1);abc[0]=5";
Expression e = jexl.createExpression( jexlExp );
List<Integer> abc = new ArrayList<>(1);
JexlContext jc = new MapContext();
//jc.set("abc", abc ); second option to add arraylist to context
Object o = e.evaluate(jc);
In JEXL, the syntax [] creates a Java array, not a List. As an array, it has a fixed size, so you cannot add values to it. However, JEXL 3.2 has a new syntax for creating an ArrayList literal. Basically, you add ... as the final element.
So in JEXL 3.2, your example could be written as:
var abc=[...];
abc.add(5);
See the JEXL literal syntax reference for more information.

Flash getter method is not able to return a 2D Array

As the title says. I tried it with a String or a normal Array and it works. But when I try to pass on my 2D Array my class won't get anything. We're talking about an Array 16 width and about 50 in length.
In my XMLLoader.as class I have this:
function getConvoArray():Array
{
trace("convoArray send");
return convoArray;
}
And in my DialogueGenerator.as class I have this:
xmlLoader = new XMLLoader("ConvoLines.xml");
convoArray = xmlLoader.getConvoArray();
I've checked if the variable convoArray is filled in the XMLLoader.as class by tracing it in a for loop; it works perfectly. But then, when I try to pass it on to the DialogueGenerator.as class it seems to be empty. I cannot excess anything and Flash doesn't give me an error or a warning.
I simply have my Array in DialogueGenerator declared as this:
public var convoArray:Array;
But I tried different ways of declaring it.
Is there a solution for this? A workaround?
For loading xml I use something like this....
var fileName:String = "ConvoLines.xml";
var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, LoaderComplete);
loader.addEventListener(IOErrorEvent.IO_ERROR, LoaderError);
loader.load(new URLRequest(fileName + "?rnd=" + Math.random()));
// we affix rand to prevent it from caching the file,
// you don't have to add the ? variable if you aren't worried about it
// updating smoothly
Then in the "LoaderComplete" function we just get the xml out. Hope that helps.
var convoXML:XML = new XML(event.target.data);

as3 passing a string from an array to a function

I have an empty array collecting a hex value thats randomly selected from another array within a function. i am trying to pull the hex value from the array that gets filled and pass it to another function to randomly change the color value of a particle system...
private var ca:Array = new Array();
private var rc:String = ca; // pseudo...this is the string that needs to get passed
public function addCursor(cursor:Cursor):void {
var cc:Array = new Array("0xFFFF33", "0xFFFFFF", "0x79DCF4", "0xFF3333", "0xFFCC33", "0x99CC33");
var rcc:String = cc[Math.floor(Math.random() * (cc.length))];
ca.push(rcc); //
trace(rcc + ' 1st array');
trace(ca + ' 2nd array');
trace(rc + ' string to pass');
// unrelated stuff happens down here...
the 1st and 2nd arrays both trace the same hex value, but i cant find the right way to capture that string...ive tried several different methods that all return 'null' ...which makes me think maybe the value is leaving the array before i try to snag it?
i removed ca.pop() which is called in a later function, just to see if rc would still return a null and it does.
rcc is a string, not an array.
So
trace(rcc + ' chosen hex as a string');
But I don't see why you need an array (ca) just to hold a single string.
This would work:
private var rc:String;
private var cc:Array = new Array("0xFFFF33", "0xFFFFFF", "0x79DCF4", "0xFF3333", "0xFFCC33", "0x99CC33");
public function addCursor(cursor:Cursor):void {
rc = cc[Math.floor(Math.random() * (cc.length))];
trace(rc + ' the chosen hex');
}

Resources