How can I convert to int array to dictionary? - arrays

Do you have any idea how I can do it?
public override IDictionary<int, DateTime> GetList()
{
var AdList = new Dictionary<int, DateTime>();
AdList = client.GetAdList(loginTicket); //it returns int array.
// I've also tried AdList =client.GetAdList(loginTicket).ToDictionary<int, DateTime>(); it doesn't work..
return AdList;
}

Well, don't have information about how you're going to get dates, or where do you get em from, but i can suggest some code:
public static IDictionary<int, DateTime> GetList()
{
var AdList = new Dictionary<int, DateTime>();
var intArr = GetAdList(); //get your int[] array or List<int>
//if method GetAdList() returns List<int> use this line
intArr.ForEach(a => AdList.Add(a, DateTime.Now));
//otherwise if method returns int[] array use this line
Array.ForEach(intArr, a => AdList.Add(a, DateTime.Now)); //populate dictionary with array elements as keys and DateTime.Now as values
return AdList;
}

Related

Property's setLabel cause NPE

I'm trying the BoundTableModel for the first time, and when I tried to set the labels of my properties, I've got a NullPointerException.
[EDT] 0:0:8,239 - Exception: java.lang.NullPointerException - null
java.lang.NullPointerException
at com.codename1.properties.PropertyBase.putClientProperty(PropertyBase.java:131)
at com.codename1.properties.PropertyBase.setLabel(PropertyBase.java:192)
at our.app.forms.UnitsForm.<init>(UnitsForm.java:54)
The NPE is throwed in this code block, but I don't understand why. The properties are public final and instanciated within the UserHistory, but no label are set there. It's better to set them later, as they need to be translated by our TranslationManager.
UiBinding ui = new UiBinding();
List<UserHistory> histories = RestManager.getHistory();
UserHistory prototype = new UserHistory();
prototype.dateCreate.setLabel("Date");
prototype.balanceUpdate.setLabel("Variation");
prototype.action.setLabel("Action");
UiBinding.BoundTableModel tb = ui.createTableModel(histories, prototype);
tb.setColumnOrder(prototype.dateCreate, prototype.balanceUpdate, prototype.action);
Table table = new Table(tb);
I don't understand how the setLabel(String) of Property can throw a NullPointerException, any idea ?
EDIT: Here are the classes which fails, the first is the AbstractEntity:
public abstract class AbstractEntity implements Serializable, PropertyBusinessObject
{
public final LongProperty<AbstractEntity> id = new LongProperty<>("id");
public final IntProperty<AbstractEntity> version = new IntProperty<>("version");
public final Property<Date, AbstractEntity> dateCreate = new Property<>("dateCreate", Date.class);
public final Property<Date, AbstractEntity> dateUpdate = new Property<>("dateUpdate", Date.class);
protected List<PropertyBase> getPropertyList()
{
List<PropertyBase> list = new ArrayList<>();
list.add(id);
list.add(version);
list.add(dateCreate);
list.add(dateUpdate);
return list;
}
protected List<PropertyBase> getExcludePropertyList()
{
List<PropertyBase> list = new ArrayList<>();
return list;
}
#Override
public PropertyIndex getPropertyIndex()
{
PropertyBase[] properties = getPropertyList().toArray(new PropertyBase[getPropertyList().size()]);
PropertyIndex index = new PropertyIndex(this, getName(), properties);
for(PropertyBase excluded : getExcludePropertyList())
{
index.setExcludeFromJSON(excluded, true);
index.setExcludeFromMap(excluded, true);
}
return index;
}
}
And here is the child class which I tried to show in a table:
public class UserHistory extends AbstractEntity
{
public final Property<User, UserHistory> user = new Property<>("user", User.class);
public final DoubleProperty<UserHistory> balanceUpdate = new DoubleProperty<>("balanceUpdate");
public final Property<String, UserHistory> action = new Property<>("action");
public UserHistory() {}
#SuppressWarnings("rawtypes")
protected List<PropertyBase> getPropertyList()
{
List<PropertyBase> list = super.getPropertyList();
list.add(user);
list.add(balanceUpdate);
list.add(action);
return list;
}
}
This won't work.
Your approach is interesting but it behaves differently from the common implementation in one significant way: the index is created lazily. So initialization work done by the index doesn't actually happen in all cases.
Worse, you create multiple indexes every time the get method is called which is also problematic.
Something that "might" work is this:
public abstract class AbstractEntity implements Serializable, PropertyBusinessObject
{
public final LongProperty<AbstractEntity> id = new LongProperty<>("id");
public final IntProperty<AbstractEntity> version = new IntProperty<>("version");
public final Property<Date, AbstractEntity> dateCreate = new Property<>("dateCreate", Date.class);
public final Property<Date, AbstractEntity> dateUpdate = new Property<>("dateUpdate", Date.class);
private PropertyIndex index;
protected AbstractEntity() {
getPropertyIndex();
}
protected List<PropertyBase> getPropertyList()
{
List<PropertyBase> list = new ArrayList<>();
list.add(id);
list.add(version);
list.add(dateCreate);
list.add(dateUpdate);
return list;
}
protected List<PropertyBase> getExcludePropertyList()
{
List<PropertyBase> list = new ArrayList<>();
return list;
}
#Override
public PropertyIndex getPropertyIndex()
{
if(idx == null) {
PropertyBase[] properties = getPropertyList().toArray(new PropertyBase[getPropertyList().size()]);
index = new PropertyIndex(this, getName(), properties);
for(PropertyBase excluded : getExcludePropertyList())
{
index.setExcludeFromJSON(excluded, true);
index.setExcludeFromMap(excluded, true);
}
}
return index;
}
}
Original answer below:
This happens if you forgot to add the property to the index object. In that case the parent object is null and meta-data can't be set.

Storing objects in array (Haxe)

How would I go about sorting an new instance of an object into an array in Haxe?
For example I have a class called weapon and in the player class I gave an array inventory.
So how would I store this?
private void gun:Weapon
gun = new Weapon; //into the array
I think you are looking for this:
private var inventory:Array<Weapon>;
This is an array of type Weapon.
To add stuff to it use push(), as seen in this example:
class Test {
static function main() new Test();
// create new array
private var inventory:Array<Weapon> = [];
public function new() {
var weapon1 = new Weapon("minigun");
inventory.push(weapon1);
var weapon2 = new Weapon("rocket");
inventory.push(weapon2);
trace('inventory has ${inventory.length} weapons!');
trace('inventory:', inventory);
}
}
class Weapon {
public var name:String;
public function new(name:String) {
this.name = name;
}
}
Demo: http://try.haxe.org/#815bD
Found the answer must be writen like this
private var inventory:Array;
With Weapon being the class name.

As3 addChild from Array

I want to add a Child which I loaded into an Array.
I got a Math.random function which returns a Number and should trigger the addChild.
And later I want to splice it into an other Array.
var ground1: MovieClip = new Ground_01;
var ground2: MovieClip = new Ground_02;
var ground3: MovieClip = new Ground_03;
var groundArray: Array = new Array();
var groundMoveArray: Array = new Array();
public function loadGroundArray() {
groundArray.push(ground1);
groundArray.push(ground2);
groundArray.push(ground3);
}
public function randomGround(minNum: Number, maxNum: Number): Number {
minNum = 0;
maxNum = groundArray.length;
return (Math.ceil(Math.random() * (0 + maxNum)));
}
ok, you are almost there... you just need a tweak to your random method to actually add the clip...
var ground1: MovieClip = new Ground_01();
var ground2: MovieClip = new Ground_02();
var ground3: MovieClip = new Ground_03();
var groundArray: Array = new Array();
var groundMoveArray: Array = new Array();
public function loadGroundArray() {
groundArray.push(ground1);
groundArray.push(ground2);
groundArray.push(ground3);
}
public function addRandomGroundToStage(): void {
var randomGroundIndex:int = Math.round(Math.random*(groundArray.length-1));
addChild(groundArray[randomGroundIndex]);
}

Finding objects index in a multidimensional array

Im making an inventory and Im adding stacks but ive hit an issue
below is what I want compared to what works
I just want to find the index of the object I pass through
myArray[0] = [item:object,StackAmmount:int]
var myArray:Array = new Array();
myArray[0] = ["name1",1];
myArray[1] = ["name2",1];
myArray[2] = ["name3",1];
trace("Name" , myArray[0][0]);
//traces "name1"
trace("Stack" , myArray[0][1]);
//traces "1"
trace("Index of Object" , myArray.indexOf("name2"));
//traces -1
// Not Working (NOT FOUND)
//How can I find the index of "item1" or "item2" in the above example
var myOtherArray:Array = new Array();
myOtherArray[0] = "name1";
myOtherArray[1] = "name2";
myOtherArray[2] = "name3";
trace("Name" , myOtherArray[0]);
//traces "name1"
trace("Index of Object" , myOtherArray.indexOf("name2"));
//traces 1
//Working
perhaps there is a better way of dealing with stacks?
Paste Bin Link: http://pastebin.com/CQZWFmST
I would use a custom class, therefore a 1D vector would be enough. The class would contain the name of the item, and the stack. You could subclass this class to override the maxStack variable of the item, and then the searches would be easier aswell, you could just iterate through the vector and check the name.
public class InventoryItem
{
protected var _name:String;
protected var _stack:int;
protected var _maxStack:int;
public function InventoryItem():void {
}
public function get name():String {
return _name;
}
public function get stack():int {
return _stack;
}
public function set stack(value:int):void {
_stack = value;
}
public function get maxStack():int {
return _maxStack;
}
}
...
public class InventoryWeapon extends InventoryItem
{
public function InventoryWeapon(__name:String, startStack:int) {
_maxStack = 64;
_name = __name;
_stack = startStack;
}
}

Convert List to Array for [WebMethod]

I understand that you cannot return a generic list in a standard .asmx webservice. However I believe you can return an array []. My problem is converting the list to an array. Can someone help?
I have a bunch of Business Object that already return a type List so I am not open to converting the original objects to Arrays...
Here is my WebMethod.
[WebMethod]
public Book[] GetBooksList()
{
List<Book> obj = new List<Book>();
BookDA dataAccess = new BookDA();
obj = dataAccess.GetBooksAll().ToArray(); //error 1 here on conversion
return obj; //error 2 here
}
Error I receive is 2 fold : Cannot implicitly convert type BookDTO.Book [] to GenericList
Cannot implicitly convert type GenericList to
Because you already declared that obj was a list, not an array. Try this instead:
[WebMethod]
public Book[] GetBooksList()
{
BookDA dataAccess = new BookDA();
List<Book> obj = dataAccess.GetBooksAll();
return obj.ToArray();
}
Or better yet:
[WebMethod]
public Book[] GetBooksList()
{
var dataAccess = new BookDA();
var obj = dataAccess.GetBooksAll().ToArray();
return obj;
}

Resources