ArrayList Collections Add new Item Sort Find Item Java example
How add item to ArrayList, sort ArrayList, search find index of item in ArrayList, min(), max() Java basic example.
import java.util.ArrayList;
import java.util.Collections;
public class MainClass {
public static void main(String[] arg) {
String[] arrayOfString = {"nothing", "Hello", "people"
, "bye-bye", "hello", "world!", "End" };
ArrayList<String> arrayList = new ArrayList<String>();
for(String s: arrayOfString)
arrayList.add(s);
Collections.sort(arrayList);
// foreach
for (String str: arrayList)
System.out.println(str);
Object objMin = Collections.min(arrayList);
System.out.println("Min is: " + objMin);
Object objMax = Collections.max(arrayList);
System.out.println("Max is: " + objMax);
int index = Collections.binarySearch(arrayList, "people");
System.out.println("Index of people is: " + index);
}
}
/*
End
Hello
bye-bye
hello
nothing
people
world!
Min is: End
Max is: world!
Index of people is: 5
*/
396LW NO topic_id
AD
Další témata ....(Topics)
entry put iterate Map HashMap Java Android
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("key27", 27);
for (Map.Entry<String, Integer> entry : map.entrySet())
{
String str = entry.getKey();
int n = entry.getValue();
}
Generate random number Android Java example source code.
Random rand = new Random();
int i = rand.nextInt() % 256; // range -255 +255
System.out.print(i + "
"); // -184
i = Math.abs(rand.nextInt() % 12); // range 0 +11
System.out.print(i); // 7
// Math.random() start with 0. e.g. 0.35981234
int nRan = (int) (Math.random()*10); // 0 - 10
// nextDouble(), nextFloat(), nextInt(), nextLong() returns 0 - 10
import java.util.Random;´
Random r = new Random();
int nRan = r.nextInt(); // 0 - 10
double dRan = r.nextDouble() * 10; // e.g. 7.496285271597397
nextDouble – return 0 - 1
nextFloat – same as double
nextInt – -2147483648 +2147483647
nextLong – -922337203685775808 +9223372036854775807
nextGaussian – 0.0 aberation 1.0.
Try this trick.
In AndroidManifest - activity tag write this code
App hold data of a views if will to rotation of device.
In AndroidManifest - activity tag write this code
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
App hold data of a views if will to rotation of device.
<activity
android:name=".Tests_Activity"
android:label="@string/app_name"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />
Check your xml files in Eclipse Graphical Editor and fix problems.
Check project setup, right click on project, select properties, Java build path and
select correct Android version etc.
Right click on project, Android Tools, Fix Project Properties
Menu Eclipse select Project ->Clean.
Check project setup, right click on project, select properties, Java build path and
select correct Android version etc.
Right click on project, Android Tools, Fix Project Properties
Menu Eclipse select Project ->Clean.
Terms Screen size, density, density independent pixel, resolution as a picture - pictogram.
Test your knowledge
Q: How to find out the phone screen size?
A: (By length of display diagonale in inch - Not to measure a diagonal of device!!!)
Q: What resolution has 720 x 1280 display?
A: (921600 pixels)
Q: What does it mean "240 dpi" screen density?
A: (Display have density 240 x 240 dots - "Tri-color LED etc." - per every physical (real) square inch. If you have icon 240x240 pixels, this will just occupy an area of one square inch on the display.)
Q: Phone have screen density 240 dpi. Image for 160 dpi screen density have size 128x 128 pixels. What will be the size of the image for 240 dpi screen density?
A: (Calculate the virtual pixels size. 128 * (240/160) = 192. You have to resize image to new size 192 x 192 physical pixels and put into folder drawable-hdpi (high) ~240dpi for phone with screen density 240 dpi. ) or use density independend pixels 128dp x 128dp.
Test your knowledge
Q: How to find out the phone screen size?
A: (By length of display diagonale in inch - Not to measure a diagonal of device!!!)
Q: What resolution has 720 x 1280 display?
A: (921600 pixels)
Q: What does it mean "240 dpi" screen density?
A: (Display have density 240 x 240 dots - "Tri-color LED etc." - per every physical (real) square inch. If you have icon 240x240 pixels, this will just occupy an area of one square inch on the display.)
Q: Phone have screen density 240 dpi. Image for 160 dpi screen density have size 128x 128 pixels. What will be the size of the image for 240 dpi screen density?
A: (Calculate the virtual pixels size. 128 * (240/160) = 192. You have to resize image to new size 192 x 192 physical pixels and put into folder drawable-hdpi (high) ~240dpi for phone with screen density 240 dpi. ) or use density independend pixels 128dp x 128dp.
Editace: 2013-12-09 10:57:01
Počet článků v kategorii: 396
Url:arraylist-collection-sort-add-java-example