Advertisement
sergAccount

Untitled

Jul 17th, 2021
906
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.66 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package com.mycompany.ex23;
  7.  
  8. import java.util.ArrayList;
  9. import java.util.LinkedList;
  10. import java.util.List;
  11.  
  12. public class Main3 {
  13.     //
  14.     public static void main(String[] args) {
  15.         //
  16.         List<String> list = new ArrayList<>();  // динам масив
  17.         // add - добавление элементы
  18.         list.add("STRING1");
  19.         list.add("STRING2");
  20.         list.add("STRING3");                
  21.         list.add("STRING4");                
  22.         // используем цикл foreach
  23.         // выводим на экран все элементы списка
  24.         for(String s: list){
  25.             System.out.println("s=" + s);
  26.         }
  27.         // получаем элемент по индексу
  28.         // get
  29.         String el1 = list.get(0);
  30.         System.out.println("el1=" + el1);
  31.         // size - кол во элементов
  32.         System.out.println("list.size=" + list.size());
  33.         //
  34.         List<Person> list2 = new ArrayList<>();  // динам масив
  35.         //list2.add("dddd");        
  36.         List<String> list3 = new LinkedList<>();  // динам масив
  37.         // int Integer
  38.         List<Integer> list4 = new ArrayList<>();  // динам масив
  39.         list4.add(1);
  40.         list4.add(2);
  41.         // выводим на экран все элементы списка
  42.         for(int s: list4){
  43.             System.out.println("s=" + s);
  44.         }
  45.     }
  46.    
  47. }
  48.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement