today during debugging of my app I found an issue in my <ArrayList<ArrayList<ArrayList<Float>>>.
My goal is to make a full copy of that ArrayList because now when I change something in the copy of the ArrayList, it makes a change in the original ArrayList too.
So I have something Like this
var first = ArrayList<ArrayList<ArrayList<Float>>>()
var second = ArrayList<ArrayList<Float>>()
var third = ArrayList<Float>()
for(j in allData) {
first.add(j.clone() as ArrayList<ArrayList<Float>>)
for(k in j)
{
second.add(k.clone() as ArrayList<Float>)
for(l in k)
{
third.add(l.toFloat())
}
}
}
normalizedAllData = first.clone() as ArrayList<ArrayList<ArrayList<Float>>>
Where normalizedAllData is <ArrayList<ArrayList<ArrayList<Float>>>
How should I create a full copy so the new ArrayList will not point to the original ArrayList?
Thanks for the help
You don't need to copy the Floats because they are immutable.
You can use nested map calls to create copies of the lists, and wrap the results in ArrayList constructors:
val normalizedAllData = ArrayList(allData.map { innerList ->
ArrayList(innerList.map { ArrayList(it) })
})
Well, I would resolved the issue in the following simple way:
I will create one helper method:
fun flattenList(nestedList: List<Any?>, flatList: MutableList<Any?>) {
nestedList.forEach { e ->
when (e) {
!is List<Any?> -> flatList.add(e)
else -> flattenList(e, flatList)
}
}
}
And this is how I would use this method:
// some sample list
val nestedList = arrayListOf(arrayListOf(arrayListOf(1f, 2f, 3f, 4f)))
// contain the flat list
val flatList = mutableListOf<Any?>()
flattenList(nestedList, flatList)
println("Nested : " + nestedList)
println("Flat : " + flatList)
Output:
Nested : [[[1.0, 2.0, 3.0, 4.0]]]
Flat : [1.0, 2.0, 3.0, 4.0]
You can use flattenList() method for any type:
Another sample:
val nestedList = listOf(
null,
1f,
listOf(2.0),
listOf(listOf(3.0, 4), 5.0),
listOf(listOf(listOf<String>("Hello Worlds"))),
listOf(listOf(listOf(6), "seven")),
"eight",
listOf<Long>()
)
val flatList = mutableListOf<Any?>()
flattenList(nestedList, flatList)
println("Nested : " + nestedList)
println("Flat : " + flatList)
Output:
Nested : [null, 1.0, [2.0], [[3.0, 4], 5.0], [[[Hello Worlds]]], [[[6], seven]], eight, []]
Flat : [null, 1.0, 2.0, 3.0, 4, 5.0, Hello Worlds, 6, seven, eight]
Related
I have a requirement to extend my plot band outside the graph and give a shape. Need to achieve the portion marked in the attached screenshot. using highchart android wrapper. While searching for the solution, I found the jsfiddle I would like to do the same thing using the android wrapper; I am using highchart library of version 6.1.4
targetimage
Link : https://jsfiddle.net/BlackLabel/e52smy16/
<script src="https://code.highcharts.com/highcharts.js"></script>
<div id="container"></div>
const renderCustomPlotBand = function(chart) {
let ren = chart.renderer,
customOptions = chart.yAxis[0].options.customPlotBand,
from = customOptions.from,
to = customOptions.to,
xEnd = chart.plotLeft + chart.plotSizeX,
xBeginig = chart.plotLeft - 40,
point1 = [xEnd, chart.yAxis[0].toPixels(from)],
point2 = [xBeginig, chart.yAxis[0].toPixels(from)],
point3 = [xBeginig, chart.yAxis[0].toPixels(to)],
point4 = [xEnd, chart.yAxis[0].toPixels(to)],
textWidth = chart;
if (customOptions.enabled) {
chart.customPlotBand = ren.g('customPlotBand').add().toFront()
chart.customText = ren.g('customText').add().toFront();
ren.path(['M', point1[0], point1[2], 'L', point2[0], point2[2], point3[0], point3[2], point4[0], point4[2]])
.attr({
'stroke-width': 1,
stroke: 'red'
}).add(chart.customPlotBand);
ren.label(customOptions.text, point2[0] - 10, point2[2] - 17).attr({
padding: 5,
fill: 'white',
rotation: -90
}).add(chart.customText);
chart.customText.translate(0, -(point2[2] - point3[2]) / 2 + chart.customText.element.getBBox().height - 3)
}
}
Highcharts.chart('container', {
chart: {
marginLeft: 80,
events: {
render() {
renderCustomPlotBand(this)
}
}
},
yAxis: [{
//Declere your custom plotband
customPlotBand: {
enabled: true,
from: 2,
to: 4,
text: 'Target'
},
title: {
text: null
},
lineWidth: 1,
gridLineWidth: 1
}],
series: [{
data: [1, 3, 6, 2, 5]
}]
});```
Unfortunately, there is no option to create a custom plotBand like in the provided example, since the renderer module is not available in the Android wrapper.
The only way of achieving similar behaviour is using static plotBand.
HIYAxis hiyAxis = new HIYAxis();
HIPlotBands plotBands = new HIPlotBands();
plotBands.setColor(HIColor.initWithName("red"));
plotBands.setFrom(2);
plotBands.setTo(4);
hiyAxis.setPlotBands(new ArrayList<>(Collections.singletonList(plotBands)));
options.setYAxis(new ArrayList<>(Collections.singletonList(hiyAxis)));
I'm trying to parse data from my json with the following kotlin code:
val text = JSONObject(URL(request).readText())
val results = text.getJSONArray("results")
val name = results.getJSONObject(5).getString("name") // org.json.JSONException: Index 5 out of range [0..1)
json
{
summary: {
queryType: "NEARBY",
queryTime: 13,
numResults: 2,
offset: 0,
totalResults: 2,
fuzzyLevel: 1,
geoBias: {
lat: -37.736343,
lon: 145.152114
}
},
results: [
{
type: "POI",
id: "AU/POI/p0/77255",
score: -0.38554,
dist: 385.5365152133808,
info: "search:ta:0323405846509-AU",
poi: {
name: "La Gourmet",
However I'm getting the following error on my 3rd line:
org.json.JSONException: Index 5 out of range [0..1)
I'm not sure why I'm getting this error. I resorted to searching for name via index because .getJSONObject("poi") doesn't take a String. This is also concerning because the data may change so I would prefer to query the JSON via String.
Any idea?
results is an array, and your code tries to get the 5th element of the array. You need to get the first element, and then you can get poi by name.
val text = JSONObject(URL(request).readText())
val results = text.getJSONArray("results")
val result0 = results.getJSONObject(0)
val poi = result0.getJSONObject("poi")
I need to add around 1000 annotation on the map. Here is my code snippet:
nsNearby.markersSetup = function() {
var location = Alloy.Globals.playgroungLocationList.locations;
var annotationData = [];
for (var i = 0,
len = Alloy.Globals.playgroungLocationList.locations.length; i < len; i++) {
var mapAnnotation = nsNearby.mapModule.createAnnotation({
latitude : location[i].latitude,
longitude : location[i].longitude,
title : location[i].name,
subtitle : location[i].street
});
if (Titanium.Platform.osname === "android") {
mapAnnotation.pincolor = nsNearby.mapModule.ANNOTATION_BLUE;
} else {
mapAnnotation.pincolor = nsNearby.mapModule.ANNOTATION_PURPLE;
}
nsNearby.mapView1.region = {
latitude : location[i].latitude,
longitude : location[i].longitude,
latitudeDelta : 1,
longitudeDelta : 1
};
annotationData.push(mapAnnotation);
}
nsNearby.mapView1.setAnnotations(annotationData);
};
In iOS, it is working fine, but in Android, the app is crashing while adding the annotation.
If I put some specific numbers of annotation, it works.
It might be because of the extra load while placing the annotations. But, is there any way to achieve this?
Thanks in advance!
I am developing a custom Cordova plugin. For some reason, I am restricted to expose all my JS objects through single JavaScript file only. Below sample JS replicating my Problem
My JS have 2 objects apple and orange, I have to export them from single file
var apple = function() {
type: "macintosh",
color: "red",
getInfo: function () {
return this.color + ' ' + this.type + ' apple';
};
var orange = function() {
color: "red2",
show: function () {
alert("test type 2 also passed dude !----");;
}
};
I am exporting them like this
var apple1 = new apple();
module.exports = apple1;
var orange1 = new orange();
module.exports = orange1;
My problem is orange1 is overriding apple1 export. How to export both apple1 and orange1 using module.exports? or there any other way?
Please provide me some inputs. Any samples are most welcome.
Following approach worked for me.
var apple1 = new apple();
var orange1 = new orange();
var myExports = module.exports;
myExports .apple1 = apple1;
myExports .orange1 = orange1;
Hello friends I am stuck at a point in sencha touch 2.0.I am creating a screen in my app in which i need to get the checkbox values(checked or unchecked). i am referring this example from sencha documentation,but i am unable to understand how to use Ext.ComponentQuery.query('what here comes?')[0].
EDIT
here's my code:-
Ext.define('MyApp.view.groupList',{
extend : 'Ext.navigation.View',
requires: ['Ext.field.Search', 'Ext.TitleBar'],
alias: "widget.groupList",
itemId: "gList",
initialize: function(){
this.callParent(arguments),
var checkBox = {
xtype: 'checkboxfield',
name : 'all',
// label: 'All',
value: 'all',
lableWidth: "0",
width: 1,
padding: '0 0 15 30',
checked: false
};
var sendBtn = {
itemId: 'sendBtn',
xtype: 'button',
text: 'Send',
ui: 'action',
handler: function() {
var check = Ext.ComponentQuery.query('navigationview')[0],
values = check.getValues();
Ext.Msg.alert(null,
"Selected All: " + ((values.all) ? "yes" : "no")
);
}
};
var topToolbar = {
xtype : "toolbar",
title : "Groups",
docked : "top",
items: [
checkBox,
{xtype: 'spacer'},
// searchBtn,
sendBtn
],
};
var list = {
xtype: "grlist",
store: Ext.getStore("grStore")
};
this.add([list,topToolbar]);
},
config: {
navigationBar: {
hidden: true,
},
}
});
I am getting the following error:-
TypeError: Result of expression 'check.getValues' [undefined] is not a function. at
file:///android_asset/www/app/view/group_list.js
So please make me understand how Ext.ComponentQuery works with some sample code or tutorial.
Thanx in advance.
Ext.ComponentQuery accepts 3 types of parameters:
xtype, eg: Ext.ComponentQuery.query('navigationview')
id, eg: Ext.ComponentQuery.query('my-nav-view-id')
attributes, eg: Ext.ComponentQuery.query('navigationview[name="my nav view"]')
No matter which type the param is of, Ext.ComponentQuery.query() always returns an array of matched components. In the example, they add [0] because it's assured that the result array contains only 1 item.
In your code, it seems that you tried to query a component of xtype navigationview, this kind of component does NOT have getValues methods (which is only available to Ext.form.Panel and derived classes). So if you want to retrieve the values, you have to use queries like this:
Ext.ComponentQuery.query('checkboxfield')[0]
Hope it helps.
Ext.ComponentQuery documentation is here...
It should not be used in this example.
This code would be more resilent and ridiculously faster as wouldn't use a global search method.
var sendBtn = {
itemId: 'sendBtn',
xtype: 'button',
text: 'Send',
ui: 'action',
scope: this,
handler: function() {
var check = this.down('#checkboxfield'),
values = check.getValue();
Ext.Msg.alert(null,
"Selected All: " + ((values.all) ? "yes" : "no")
);
}
};
Also I've corrected the code example, the method is getValue()
Note:
I know that this answer is so out of date it's not relevent, however the accepted method is encouraging the use of Ext.ComponentQuery where it's not needed which is simply awful code and has logical code presuming that ui will remain in the same set format.