Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data collections Android Club 2015. Agenda Array ArrayList HashMap.

Similar presentations


Presentation on theme: "Data collections Android Club 2015. Agenda Array ArrayList HashMap."— Presentation transcript:

1 Data collections Android Club 2015

2 Agenda Array ArrayList HashMap

3 Array Array is container object that holds a fixed number of values of a single type

4 Why we need array? We need array to store list of data in one object

5 When to use array? When you know exact number of data in list When all data in the same data type

6 int array: example int[] numbers = {3, 6, 9}; System.out.println(numbers[0]);

7 int array: practice Create array of numbers: from 1 to 10 Print 3 rd number Print 6 th number Print 9 th number

8 String array: example String[] weekdays = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"}; System.out.println(weekdays[1]);

9 String array: practice Create array of strings: months Print June using array

10 boolean array: example boolean[] isOrel = {true, false, false}; System.out.println(isOrel[1]);

11 boolean array: practice Create boolean array: isReshka Size: 5 Print 4 th value

12 char array: example char[] letters = {'a', 'b', 'c'}; System.out.println(letters[1]);

13 char array: practice Create char array: alphabet Print 10 th letter Print 20 th letter

14 Empty array: example String[] names = new String[3]; names[0]="Vadim"; names[1]="Jonik"; names[2]="Timur"; System.out.println(names[1]);

15 Empty array: practice Create empty String array: fruit Size: 4 0 – Apple 1 – Pineapple 2 – Banana 3 - Kiwi

16 Array length: example int[] letters = {3, 6, 9}; System.out.println(letters.length);

17 Array length: practice Use your alphabet array Print array length

18 Array iteration: example String[] names = {"Nescafe", "Jacobs", "Maccofe"}; for (String name : names) { System.out.println(name); }

19 Array iteration: practice Use months array Print all months

20 Disadvantages of array You set size when you declare. Then you can not change its size You can use only same data type

21 What is ArrayList? An ArrayList is a dynamic data structure, meaning items can be added and removed from the list.

22 When to use ArrayList? When you are not sure about size When your data collection is dynamic

23 Integer ArrayList: example ArrayList numbers = new ArrayList (); numbers.add(1); numbers.add(2); numbers.add(3); System.out.println(numbers.get(0));

24 Integer ArrayList: practice Create int ArrayList: numbers Add numbers from 1 to 10 Print 5 th number

25 String ArrayList: example ArrayList weekdays = new ArrayList (); weekdays.add("Monday"); weekdays.add("Tuesday"); weekdays.add("Wednesday"); System.out.println(weekdays.get(1));

26 String ArrayList: practice Create String ArrayList: months Print June using ArrayList

27 Boolean ArrayList: example ArrayList isOrel = new ArrayList (); isOrel.add(true); isOrel.add(false); System.out.println(isOrel.get(0));

28 Boolean ArrayList: practice Create Boolean ArrayList: isReshka Size: 5 Add boolean values Print 3 rd value

29 Character ArrayList: example ArrayList letters = new ArrayList (); letters.add('a'); letters.add('b'); letters.add('c'); System.out.println(letters.get(2));

30 Character ArrayList: practice Create Character ArrayList: alphabet Add all letters to ArrayList Print 10 th letter Print 20 th letter

31 ArrayList remove: example ArrayList names = new ArrayList (); names.add("Vadim"); names.add("Jonik"); names.add("Timur"); names.remove(1); System.out.println(names);

32 ArrayList remove: practice Use String ArrayList: months Remove 5 th item Print ArrayList

33 ArrayList size: example ArrayList names = new ArrayList (); names.add("Vadim"); names.add("Jonik"); names.add("Timur"); System.out.println(names.size());

34 ArrayList size: practice Use previous String ArrayList: months Print size of months ArrayList

35 ArrayList iteration: example Use months ArrayList Print all months

36 HashMap Unordered list Based on key-value

37 HashMap: example HashMap countryCodes = new HashMap (); countryCodes.put("UZ", "Uzbekistan"); countryCodes.put("JP", "Japan"); countryCodes.put("US", "United States"); System.out.println(countryCodes.get("UZ" ));

38 HashMap: practice Create HashMap: elements Key: String Value: String Add values: H – Hydron O – Oxygen N – Nitrogen Print Oxygen

39 HashMap: practice 2 Create Hashmap: httpCodes Key: int Value: String 404 – Not found 403 – Forbidden 400 – Bad request Print “Not found”

40 HashMap: practice 3 Create HashMap: capitals Key: String Value: String Uzbekistan – Tashkent Japan – Tokyo US – Washington Print Japan’s capital city

41 Questions? Any questions?

42 Review Lynda.com - 8. Using Data Collections

43 Thank you! Thank you very much for your attention!


Download ppt "Data collections Android Club 2015. Agenda Array ArrayList HashMap."

Similar presentations


Ads by Google