2014-01-23 2 views
-2

이 코드는 임의의 숫자를 샘플링하여 배열에 연결하고, 홀수를 계산 한 다음 사용자가 정수를 선택하여 일치하는지 확인하도록합니다. 이것은 훌륭한 배열로만 작동하지만 ArrayLists와 함께 작동하도록 변환하려고합니다. 가장 쉬운 방법은 무엇입니까?배열 대신 배열 목록을 사용하도록 변환하려면 어떻게해야합니까?

import java.util.Scanner; 
import java.util.Random; 

public class ArrayList { 
    public static void main(String[] args) { 
     // this code creates a random array of 10 ints. 
     int[] array = generateRandomArray(10); 

     // print out the contents of array separated by the delimeter 
     // specified 

     printArray(array, ", "); 

     // count the odd numbers 

     int oddCount = countOdds(array); 

     System.out.println("the array has " + oddCount + " odd values"); 
     // prompt the user for an integer value. 
     Scanner input = new Scanner(System.in); 
     System.out.println("Enter an integer to find in the array:"); 
     int target = input.nextInt(); 

     // Find the index of target in the generated array. 

     int index = findValue(array, target); 

     if (index == -1) { 
      // target was not found 
      System.out.println("value " + target + " not found"); 
     } else { 
      // target was found 
      System.out.println("value " + target + " found at index " + index); 
     } 

    } 

    public static int[] generateRandomArray(int size) { 
     // this is the array we are going to fill up with random stuff 
     int[] rval = new int[size]; 

     Random rand = new Random(); 

     for (int i = 0; i < rval.length; i++) { 
      rval[i] = rand.nextInt(100); 
     } 

     return rval; 
    } 

    public static void printArray(int[] array, String delim) { 

     // your code goes here 
     for (int i = 0; i < array.length; i++) { 
      System.out.print(array[i]); 
      if (i < array.length - 1) 
       System.out.print(delim); 
      else 
       System.out.print(" "); 
     } 
    } 

    public static int countOdds(int[] array) { 
     int count = 0; 

     // your code goes here 
     for (int i = 0; i < array.length; i++) { 
      if (array[i] % 2 == 1) 
       count++; 
     } 

     return count; 
    } 

    public static int findValue(int[] array, int value) { 
     // your code goes here 
     for (int i = 0; i < array.length; i++) 
      if (array[i] == value) 
       return i; 
     return -1; 

    } 
} 
+3

음 ... 'ArrayList'에 대한 Javadoc을 읽어서 필요한 변경 사항이 최소화되어 있는지 확인하십시오. –

+0

이 게시물 [배열 (T [])에서 ArrayList (ArrayList )를 만드는 방법] http://stackoverflow.com/questions/157944/how-to-create-arraylist-arraylistt- 배열에서 - t –

답변

0

두 가지 기능을 다시 작성합니다. 아마도 유용 할 것입니다.

public static List<Integer> generateRandomList(int size) { 
    // this is the list we are going to fill up with random stuff 
    List<Integer> rval = new ArrayList<Integer>(size); 
    Random rand = new Random(); 
    for (int i = 0; i < size; i++) { 
     rval.add(Integer.valueOf(rand.nextInt(100))); 
    } 
    return rval; 
} 

public static int countOdds(List<Integer> rval) { 
    int count = 0; 
    for (Integer temp : rval) { 
     if (temp.intValue() % 2 == 1) { 
      count++; 
     } 
    } 
    return count; 
} 
관련 문제