-2

This code takes a random sampling of numbers, plugs them into an array, counts the odd numbers, and then lets the user pick an integer to see if it matches. This works great now with just arrays, but I want to convert it to work with ArrayLists. What's the easiest way to do this?

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;

    }
}
Ashish Aggarwal
  • 3,018
  • 2
  • 23
  • 46
  • 3
    Umm... start by reading the Javadoc for `ArrayList` to see that the changes required are minimal? – Jim Garrison Jan 23 '14 at 04:14
  • You may need to refer this post [How to create ArrayList (ArrayList) from array (T[])] http://stackoverflow.com/questions/157944/how-to-create-arraylist-arraylistt-from-array-t – Fate Chang Jan 23 '14 at 04:16

1 Answers1

0

I rewrite two of your functions,maybe they will useful for you.

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;
}
mojiayi
  • 187
  • 1
  • 2
  • 11