I want to Put two arrays value in LinkedHashMap as key-value.
Here is the snippet that I'm using:
String[] s = answer.split("\\,");
String[] ss = aa.split("\\,");
System.out.println(ss.length); -->prints 3
System.out.println(s.length); -->prints 3
What I want is to put s values as Key and ss values as Value in HashMap.
I'm trying to write code.
for(int i=0;i<s.length;i++){
for(int j= 0;j<ss.length;j++){
if(s[i].length()==s[j].length()){
testMap.put(s[i], ss[j]);
}
}
}
But unable to Put into Map. What I've done wrong?
And I'm using LinkedHashMap to preserve the order of Insertion.
Here is the solution:
for(int i=0;i<s.length;i++){
testMap.put(s[i], ss[i]);
}
I just have to change my loop condition to this. Instead of using two for loops.
Thanks everybody.
Use this code, It will add accordingly
String answer = "ID,NAME,VALUES";
String aa = "1,KLAXXON,ROMEO";
String[] s = answer.split("\\,");
String[] ss = aa.split("\\,");
for (int i = 0; i < s.length; i++) {
testMap.put(s[i], ss[i]);
}
Output:
{ID=1, NAME=KLAXXON, VALUES=ROMEO}
Related
I've built a 2d array of ImageViews, and I want to be able to print out the indices of the ImageView when I click it (and use that information to change the pictures).
I've got the following code right now, but I don't know how to get it to print the x and y values of its location in the 2d array.
ImageView[][] mainGrid = new ImageView[8][8];
...
for (int x = 0; x < 8; x++) {
for (int y = 0; y < 8; y++) {
mainGrid[x][y] = new ImageView(image);
mainGrid[x][y].setOnMouseClicked(createMouseHandler());
}
}
...
private EventHandler<? super MouseEvent> createMouseHandler() {
return event -> {
System.out.println("Clicked");
}
}
Any help is greatly appreciated! Thank you!
The most general solution to this problem is to use the getProperties() method of the Node class. You can use that to store any user information in a node you like and then later retrieve that if you need it. In your case store the indices and then retrieve them in your mouse handler.
I have an ArrayList and want to assign its elements to an array. I can do it using classic old java code:
List<String> list = new ArrayList<String>();
list.add("Mango");
list.add("Apple");
list.add("Banana");
list.add("Grapes");
String[] optionCode = new String[4];
// Before Java 7
System.out.println("Before Java 7:");
for (int i = 0; i < list.size(); i++) {
optionCode[i] = list.get(i);
}
for (int i = 0; i < optionCode.length; i++) {
System.out.println("Element " + i + " is: " + optionCode[i]);
}
Now I want to use new java 8 features: forEach and lambda method:
list.stream().forEach(i -> {
optionCode[i] = i;
System.out.println(i);
});
It complains: Type mismatch: cannot convert from String to int
How can I fix it?
You may try using the stream toArray() method here:
String[] optionCode = list.stream().toArray(String[]::new);
System.out.println(Arrays.toString(optionCode));
This prints:
[Mango, Apple, Banana, Grapes]
Tim Biegeleisen already suggested the solution to your problem.
The reason, however, that it doesn't work, is that you are trying to access both the index and the element at that index. With forEach, you don't have access to the index.
If you really need to use the index sometime (because, for example, you want to filter out the element at index n), you could use IntStream:
IntStream.range(0, list.size())
.filter(i -> i != n)
.mapToObj(i -> list.get(i)) // Or mapToObj(List::get)
.toArray(String[]::new);
Here is an alternative solution without using forEach.
String[] optionCode = list.toArray(n -> new String[n]);
friends. I have the following issue. I have two observable arrays.
self.NamesArray= ko.observableArray([{"name: John"}, {"name: Mario"}]);
and
self.ValueArray = ko.observable([]);
I would like to loop through the NamesArray and add only the name values to the ValueArray.
So the output ValueArray should contain the following elements in the end:
{John, Mario}
How can this happen? I am very new to JS and I am just researching the Knockout library. Any help with working example will be greatly appreciated. Thanks.
Fiddle: http://jsfiddle.net/PsyComa/RfWVP/
This really depends on your intention behind doing this.
If you want do to this just once, simply iterate over the first array:
// Load current values of the observables
var n = self.NamesArray(), v = self.ValueArray();
// Push all names from n to v
for (var i = 0; i < n.length; i++) {
v.push( n[i].name );
}
// Update the "values" observable
self.ValueArray(v);
The downside with this is that "ValueArray" does not get updated whenever "NamesArray" changes. If you want "ValueArray" to be an array containing all names that can be found in "NamesArray" (and only those!), you can use a computed observable instead:
self.ValueArray = ko.computed(function() {
var n = self.NamesArray(), v = [];
// ...same for loop as above...
return v;
});
I want to Loop through the array of integers and want to remove the items in the TLToProcess list which i have stored in the array of integers
here is the code
I want to remove only the selected in the list integer
iSize.add(TLToProcess.size());
if(TLToProcess[i].Scan_In1__c==null)
{
if(TLToProcess[i].typew__c=='Pending')
{
TLForMissingHHhh.add(TLToProcess[i]);
}
}
else if ( c[i].Scan_In1__c!=null)
{
if (TLToProcess[i].typew__c=='Pending' )
{
TLToProcess[i].typew__c='Processed';
}
}
}
Now i want to remove record 1 by 1 from TLToProcess using
remove() can any body tell me how to do it.
Thanks
Anu
Not sure I understand your problem, but if what you're trying to avoid is modifying your List of integers inside a loop and getting this error: {"Collection was modified; enumeration operation may not execute."} you can create a copy of your List(.ToList()) and use it to iterate, and this way you can call Remove() safely.
List<Int32> arr = new List<Int32>();
for (int i = 0; i < 10; i++)
{
arr.Add(i);
}
foreach(var o in arr.ToList())
{
arr.Remove(o);
}
Is that the intent?
I am running into some trouble adding an array into another array to create a multi-dimensional array.
The code appears as below:
var slideDataArray:Array = new Array();
var slideShowDataArray:Array = new Array();
slideDataArray[0] = xmlData.SlideShowParameters.SlideShowImagesDirectory;
slideDataArray[1] = xmlData.SlideShowParameters.SlideShowTimeInterval.valueOf();
slideDataArray[2] = xmlData.SlideShowParameters.SlideShowWidth.valueOf();
slideDataArray[3] = xmlData.SlideShowParameters.SlideShowHeight.valueOf();
slideDataArray[4] = slides.length();
slideShowDataArray[0] = slideDataArray;
for (i = 0; i < slides.length(); i++) {
// Read data from Slides tag in the XML file into slideDataArray
slideDataArray[0] = slides[i].SlideImage.toString();
slideDataArray[1] = slides[i].SlideText.toString();
slideDataArray[2] = slides[i].SlideLink.toString();
// Input the data from slideDataArray into the array for the slideshow (slideShowDataArray)
slideShowDataArray[i + 1] = slideDataArray;
}
// end of FOR loop
I am looking for a means of placing the slideDataArray into a 'slot' or value of slideShowDataArray so that I can in the end pass the slideShowDataArray as a parameter to another function.
As of now, the last slideDataArray appears 11 times (the loop runs 11 times) in slideShowDataArray and the way the code is written the slideDataArray is unique every iteration of the loop.
Any help is appreciated.
Thanks in advance...
Remember you are not adding an array, but a reference to slideDataArray to your multidimensional array. Each reference points to the same array - which just contains different values on every iteration of the loop. In other words: Every time you add that reference, you "link" to the same address in memory.
To get around this, move the inner part of the loop to a separate function and create a new local array on every call:
function createDataArray ( slide:Object ) : Array {
var slideDataArray:Array = [];
slideDataArray[0] = slide.SlideImage.toString();
slideDataArray[1] = slide.SlideText.toString();
slideDataArray[2] = slide.SlideLink.toString();
return slideDataArray;
}
Then call it from your loop:
for (i = 0; i < slides.length(); i++) {
slideShowDataArray.push( createDataArray (slides[i]) );
}
You should end up with 11 unique arrays instead of one array that is overwritten 11 times.