I have arraylist like {23,45,44,78}. I have tried to get value at particular position and get difference of two but is there any best way to get diference of 23-45, 45-44 and so on
The pre-Java 8 way of doing this would be to just use a for loop:
for (int i=0; i < list.size()-1; ++i) {
System.out.println(list.get(i) - list.get(i+1));
}
Demo
There might be a slightly more compact way of doing this using streams.
Related
I am receiving a SoapObject with 2 properties having the same name. I want to remove one of them from the Object, and I am not sure how do I achieve that.
The purpose is, I actually want to read both the properties like:
soapObject.getProperty("PropertyName");
I have successfully tried using a for loop:
for(int i = 0; i < soapObject.getPropertyCount(); i++)
soapObject.getProperty(atIndex);
But, it doesn't serve my purpose. I need to retrieve them by name. So, how can I retrieve both?
I tried calling the method twice, but it returns the same values twice.
Have you already tried with the for each loop? It allows you to access both the key and, consequently, the value.
Here you can find an example from which you can start:
for (K key : map.keySet()) {
System.out.println(key + "=" + map.get(key));
}
If i have understood the problem statement correctly
you can remove the duplicate soap object by one of the following methods,(but first add it into an arraylist)
Using Iterator
Using LinkedHashSet. A better way (both time
complexity and ease of implementation wise) is to remove duplicates from an ArrayList is to convert it into a Set that does not allow duplicates
Using Java 8 Stream.distinct() You can use the distinct() method from the Stream API
I have a list of objectId ,I want to use these objectId to use these in recycler view. Below is the given code I am using to get my desired results
for (int i = 0; i < wishList.size(); i++) {
q2.whereEqualTo("objectId", wishList.get(i));
}
But the problem is only last index values of wishList is applying to my queries, my guess is previous index values are being overriden .
If any other solution please suggest (without using parse relation)
I think you can just do
q2.whereContainedIn("objectId", wishList)
I am writing an android app that has several large multi-dimensional arrays of data like float[][][][], which should be loaded at the beginning of application run. So I need to read and load them from files, stored in phone storage.
The problem I have with typical solution of reading from files, is that it takes so long to read and load all of them and I am looking for a faster option.
I decided to give a try to Json for mentioned purpose, but I couldn't find any way to store multi-dimensional arrays like float[][][][]. Can anybody propose any clues to help me? Also if you know a better solution rather than Json for my situation, I would really appreciate to hear.
Thanks
A multidimensional array is simply an array of arrays of arrays (repeat for additional dimensions).
Creating a 2d array of floats would go something like this:
float[][] array; // your array
JSONArray outerJsonArray = new JsonArray();
for (int i=0; i < array.length; i++) {
JSONArray innerJsonArray = new JsonArray();
for (int j=0; j < array[i].length; j++) innerJsonArray.put(array[i][j]);
outerJsonArray.put(innerJsonArray);
}
To convert higher dimensional arrays to json, you simply increase the nesting of the loop to match the dimensionality of the array.
so I'm currently writing an app for android, and im still a noob in java/android.
Anyways i have this 2 strings, one with names and the other with emails, and i want to output them in a listview with a custom adapter.
It works fine so far but i dont know how to set the items dynamically (with a for loop).
To create the adapter and so on, I used this tutorial:
http://www.ezzylearning.com/tutorial/customizing-android-listview-items-with-custom-arrayadapter
I simply changed the ImageView to a second TextView.
In the tutorials code there are 5 items added to the list, but i need them dynamically, since Im not always having the same amount of name+emails to output
I already tried putting it in a for-loop by doing:
Weather weather_data[] = new Weather[names.length];
for(int z=0; z == names.length){
Weather[z]={new Weather(names[z], emails[z])};
}
I also tried it with adding "new" infront and trying to set everything null before, basically trial&error since i dont know much about it.
So can anyone tell me how I add the items dynamically?
(Ps: sorry if I used wrong names to describe anything)
This should work
Weather weather_data[] = new Weather[names.length];
for(int z=0; z < names.length; z++){
weather_data[z] = new Weather(names[z], emails[z]);
}
Give this a read to learn how for loops work
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html
and this one for arrays
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
try this
ArrayList<Weather> weatherData = new ArrayList<>();
for (int i=0; i < names.length(); i++){
weatherData.add(new Weather(names[i], emails[i]));
}
Then when you need it as a Weather[] use weatherData.toArray()
Using java for Android, I have come across a problem:
for(int a = 0; a < 26; a++){
textViewArray[a] = (TextView) findViewById(R.id.(alphabet[a]));
}
I have 26 TextViews in an xml file with the ids A, B, C, D, ...Z I need the above to become R.id.A, R.id.B, R.id.C, ...R.id.Z when it is run. The above is one of my attempts which for obvious reasons doesn't work. I have thought of using
int[26] = new int[]{R.id.A, R.id.B, R.id.C .... R.id.Z};
then using those array entries in the above for loop but is there a neater way to do it? Possibly converting a string ( "R.id." + (alphabet[a]) ) to code and letting it run as usual to find the id?
P.S. alphabet is a String array containing A, B, C ... Z in the above example
Resources.getIdentifier(). Keep in mind that this function is quite slow, so don't use it unless you have to.
In your case, I'd stick with the array you proposed, that's the better approach.