Populating 2D Array with songs and rating - arrays

So I am trying to write a 2D array using Java that stores the names of songs and its rating in each of its slots. I have tried doing this with nested for-each loops but it doesn't work. Is there a way to store the songs that works?
public class Jukebox
{
String[][] songList = new String[2][3];
public Jukebox()
{
for ( String[] row : songList)
{
for ( String column : row)
{
songList[0][0] = new Jukebox( "Jet Airliner" );
songList[0][1] = new MySong( "Slide", 4 );
songList[0][2] = new MySong( "Tom Sawyer", 3 );
songList[0][3] = new MySong( "Purple Rain", 2 );
songList[1][0] = new MySong( "Sing a Song", 1 );
songList[1][1] = new MySong( "Baba O'Riley", 5 );
songList[1][2] = new MySong( "Jumper", 4 );
songList[1][3] = new MySong( "Car Wash", 3 );
songList[2][0] = new MySong( "Kung Fu Fighting", 2 );
songList[2][1] = new MySong( "Right as Rain", 4 );
songList[2][2] = new MySong( "Beat It", 5 );
songList[2][3] = new MySong( "Bust a Move", 4 );
}
}
}
}

First, what problem do you need to solve? Wouldn't the use of a HashMap be more desirable? Suppose you have 5 songs:
Map<String, Integer> songs = new HashMap<>();
//populate the map as
songs.put("Slide", 4);
songs.put("Purple rain", 2);
songs.put("Jumper", 3);
//iterate through the map as
for (Map.Entry<String, Integer>entry : songs.entrySet()) {
System.out.println("Song: " + entry.getKey() + " rating: " + entry.getValue());
}
Hope this helps you.

Related

Array Map multidimensional, get values from 3 array Java

Let say I have 3 arrays,
compCD = ["909", "908", "080", "901"];
contNO = ["09999", "08888", "00777", "00666"];
pomNum = ["A01", "A02", "A03", "A04"];
How can I insert into jsonMap each separated Values like
[
{
"compCD" = "909",
"contNO" = "09999",
"pomNum" = "A01"
},{
"compCD" = "908",
"contNO" = "08888",
"pomNum" = "A02"
}
]
we can assume that each array size is the same. The first of each array will bi insert first to map.
How we can solve those with minimum loop.
var compCD = ["909", "908", "080", "901"];
var contNO = ["09999", "08888", "00777", "00666"];
var pomNum = ["A01", "A02", "A03", "A04"];
var jsonMap=[];
for(var i=0; i<compCD.length; i++) {
jsonMap.push(
{compCD:compCD[i], contNO:contNO[i], pomNum:pomNum[i]}
);
};
console.log(jsonMap);
var compCD = ["909", "908", "080", "901"];
var contNO = ["09999", "08888", "00777", "00666"];
var pomNum = ["A01", "A02", "A03", "A04"];
var jsonMap=compCD.map(
(currentValue, index)=>[currentValue, contNO[index], pomNum[index]]
);
console.log(jsonMap);

MultiList not showing complete text (Codename One)

Im using a MultiList to display some result from a web service but the address is not showing up completely.
Please this picture
The first address should be "Jalan Gertak Merah, 80000 Johor Bahru, Johor, Malaysia" and not "Jalan Gertak Merah, 80"
How can I correct this please.
This is my code:
Style s = UIManager.getInstance().getComponentStyle("Button");
FontImage p = FontImage.createMaterial(FontImage.MATERIAL_PORTRAIT, s);
EncodedImage placeholder = EncodedImage.createFromImage(p.scaled(p.getWidth() * 3, p.getHeight() * 4), false);
f.setTitle("Tourist Attractions");
getattractive();
ArrayList arr = (ArrayList) response.get("results");
for (Object m : arr) {
Map ma = (Map) m;
address = (String) ma.get("formatted_address");
name = (String) ma.get("name");
icon = (String) ma.get("icon");
data.add(createListEntry(name, address, icon));
}
DefaultListModel<Map<String, Object>> model = new DefaultListModel<>(data);
MultiList ml = new MultiList(model);
ml.getUnselectedButton().setIconName("icon_URLImage");
ml.getSelectedButton().setIconName("icon_URLImage");
ml.getUnselectedButton().setIcon(placeholder);
ml.getSelectedButton().setIcon(placeholder);
findContainer(f).add(BorderLayout.CENTER, ml);
c.addComponent(bn);
bn.addActionListener((ActionEvent evt) -> {
System.exit(0);
});
findContainer(f).add(BorderLayout.SOUTH, c);
f.getComponentForm().revalidate();

How to sort array of custom objects by customised status value in swift 3?

lets say we have a custom class named orderFile and this class contains three properties.
class orderFile {
var name = String()
var id = Int()
var status = String()
}
a lot of them stored into an array
var aOrders : Array = []
var aOrder = orderFile()
aOrder.name = "Order 1"
aOrder.id = 101
aOrder.status = "closed"
aOrders.append(aOrder)
var aOrder = orderFile()
aOrder.name = "Order 2"
aOrder.id = 101
aOrder.status = "open"
aOrders.append(aOrder)
var aOrder = orderFile()
aOrder.name = "Order 2"
aOrder.id = 101
aOrder.status = "cancelled"
aOrders.append(aOrder)
var aOrder = orderFile()
aOrder.name = "Order 2"
aOrder.id = 101
aOrder.status = "confirmed"
aOrders.append(aOrder)
Question is: How will I sort them based on status according to open, confirm, close and cancelled?
You have to provide a value that will yield the appropriate ordering when compared in the sort function.
For example:
extension orderFile
{
var statusSortOrder: Int
{ return ["open","confirmed","closed","cancelled"].index(of: status) ?? 0 }
}
let sortedOrders = aOrders.sorted{$0.statusSortOrder < $1. statusSortOrder}
In your code you should make an array to store each aOrder with aOrders.append(aOrder) at each aOrder defination.
Then sort it with below code, refor this for more.
aOrders.sorted({ $0.status > $1.status })
The answer for swift 3 is as following
Ascending:
aOrders = aOrders.sorted(by:
{(first: orderFile, second: orderFile) -> Bool in
first.status > second.status
}
)
Descending:
aOrders = aOrders.sorted(by:
{(first: orderFile, second: orderFile) -> Bool in
first.status < second.status
}
)

how to add an attribute to an existing array dynamically in Flex

I have an 2D array as:
Asia India 100 200
Asia China 200 300
I need to add an attribute named "state" with value false to this above array (internally) as an additional column to the above array where my output is :
Asia India 100 200 state:false
Asia China 200 300 state:false
so that I can give the additional column's attribute name "state" to item renderer.
How to get the same.?Is it possible?
For lines:
var originalArray:Array = //your original array
var newArray:Array = new Array();
for each ( var o:Object in originalArray){
o.state = "false";
newArray.push(o);
}
originalArray = newArray;
trace(originalArray[0]) // Asia India 100 200
trace(originalArray.state) //false
using Array.map() should work too.
For invidual objects:
protected function addStates():void
{
var originalArray:Array = //your original array
var newArray:Array = new Array();
for each( var o:Object in originalArray){
var tempArray:Array = new Array();
for each(var element:* in o){
var newObject:Object = new Object();
newObject.element = element;
newObject.state = "false";
tempArray.push(newObject);
}
newArray.push(tempArray);
}
originalArray = newArray;
trace(originalArray[0][1].element); // India
trace(originalArray[0][1].state); // false
}
If someone has a better/faster way I am interested to hear it.

Qooxdoo form elements and getSelection()

Here's my code:
var sb = new qx.ui.form.SelectBox();
sb.add( new qx.ui.form.ListItem("English") );
sb.add( new qx.ui.form.ListItem("Nederlands") );
sb.add( new qx.ui.form.ListItem("Deutsch") );
sb.add( new qx.ui.form.ListItem("français") );
sb.add( new qx.ui.form.ListItem("Српски") );
How do I use setSelection() to select "Deutsch", and what if the items are numeric values? Can I also set values for these labels or is SelectBox() limited to labels?
For example:
value: en, label: English
value: de, label: Deutsch
etc.
Take a look at the example code below.
You can specify a model with each ListItem for storing additional information. It can act as value property on form items for example. See http://demo.qooxdoo.org/1.0.x/apiviewer/#qx.ui.form.ListItem
var selectBox = new qx.ui.form.SelectBox();
selectBox.add( new qx.ui.form.ListItem("English", null, "en" ));
selectBox.add( new qx.ui.form.ListItem("Nederlands", null, "nl" ));
var defaultItem = new qx.ui.form.ListItem("Deutsch", null, "de" );
selectBox.add(defaultItem );
selectBox.add( new qx.ui.form.ListItem("français", null, "fr"));
selectBox.add( new qx.ui.form.ListItem("Српски", null, "ru"));
selectBox.setSelection([defaultItem]);
selectBox.addListener("changeSelection", function(e) {
//Read model data from listitem
this.debug("changeSelection: " + e.getData()[0].getModel());
});
Maybe this example will be useful for you too:
var sb = new qx.ui.form.SelectBox();
var a = ["English", "Nederlands", "Deutsch", "Français", "Српски"];
var model = new qx.data.Array(a);
var controller = new qx.data.controller.List(model, sb);
controller.setSelection(model.slice(0,3));
At last line model.slice(0,3) returns subarray of model with three elements: from "English" to "Deutsch". And last element in this subarray will be "selected" by default.
See "Data Binding" in qooxdoo's manual for details.

Resources