CodeGym /행동 /자바 구문 /ArrayList 및 제네릭을 사용하는 예

ArrayList 및 제네릭을 사용하는 예

자바 구문
레벨 7, 레슨 8
사용 가능

"실제로 유용한 것부터 시작하는 것은 어떻습니까? 이제 ArrayList와 제네릭을 작동시킬 수 있는 몇 가지 방법을 보게 될 것입니다."

"예시 1:"

키보드에서 숫자 목록 읽기
public static void main(String[] args) throws IOException
{
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in) );
    ArrayList<Integer> list = new ArrayList<Integer>() ;

    while (true)
    {
        String s = reader.readLine();
        if (s.isEmpty()) break;
        list.add(Integer.parseInt(s));
    }
}

"예시 2:"

위와 동일하지만 짝수는 목록의 끝에 추가되고 홀수는 목록의 시작 부분에 추가됩니다.
public static void main(String[] args) throws IOException
{
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    ArrayList<Integer> list = new ArrayList<Integer>();

    while (true)
    {
        String s = reader.readLine();
        if (s.isEmpty()) break;

        int x = Integer.parseInt(s);
        if (x % 2 == 0)  // Check that the remainder is zero when we divide by two
            list.add(x);         // Add to the end
        else
            list.add(0, x);      // Add to the beginning           
    }
}

"예시 3:"

5보다 큰 모든 숫자 삭제:
public static void main(String[] args) throws IOException
{
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in) );
    ArrayList<Integer> list = new ArrayList<Integer>();

    list.add(1);
    list.add(7);
    list.add(11);
    list.add(3);
    list.add(15);

    for (int i = 0; i < list.size(); ) // We moved the statement that increases i to inside the loop 
    { 
        if (list.get(i) > 5)
            list.remove(i);  // Don’t increase i if we deleted the current element   
        else
            i++;
    }
}

"예시 4:"

배열을 짝수와 홀수 두 부분으로 나눕니다.
public static void main(String[] args) throws IOException
{
    // Static initialization of the array
    int[] data = {1, 5, 6, 11, 3, 15, 7, 8};  

    // Create a list where all elements are Integers 
    ArrayList&ltInteger> list = new ArrayList&ltInteger> ();   

    // Use the array to fill the list
    for (int i = 0; i < data.length; i++) list.add(data[i]);  

    ArrayList&ltInteger> even = new ArrayList&ltInteger>();  // Even numbers
    ArrayList&ltInteger> odd = new ArrayList&ltInteger>();    // Odd numbers

    for (int i = 0; i < list.size(); i++)
    {
        Integer x = list.get(i);
        if (x % 2 == 0)    // If x is even
            even.add(x);   // Add x to the collection of even numbers  
        else
            odd.add(x);    // Add x to the collection of odd numbers
    }
}
2
과제
자바 구문, 레벨 7, 레슨 8
잠금
Code entry
Your attention, please! Now recruiting code entry personnel for CodeGym. So turn up your focus, let your fingers relax, read the code, and then... type it into the appropriate box. Code entry is far from a useless exercise, though it might seem so at first glance: it allows a beginner to get used to and remember syntax (modern IDEs seldom make this possible).

"예시 5:"

목록 병합
public static void main(String[] args) throws IOException
{
    ArrayList<Integer> list1 = new ArrayList<Integer>();   // Create a list  
    Collections.addAll(list1, 1, 5, 6, 11, 3, 15, 7, 8);   // Fill the list

    ArrayList<Integer> list2 = new ArrayList<Integer>();
    Collections.addAll(list2, 1, 8, 6, 21, 53, 5, 67, 18);

    ArrayList<Integer> result = new ArrayList<Integer>();

    result.addAll(list1);   // Add all values from each list to the new list
    result.addAll(list2);

    for (Integer x : result)   // A fast way to loop over all elements, only for collections
    {
        System.out.println(x);
    }
}

"멋지다! 이제 디에고가 나에게 비슷한 운동을 트럭에 실어줄까?"

"네 그는 그럴 거에요."

코멘트
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION