Function not filling ArrayList and/or Function not printing ArrayList. Program r
ID: 644005 • Letter: F
Question
Function not filling ArrayList and/or Function not printing ArrayList. Program runs fine, but array not in output.Please Help:
import java.util.Random;
import java.util.ArrayList;
public class StackStuff<T>{
static int SIZE = 10;
public static ArrayList<Integer> fillArray(int n){
ArrayList<Integer> array = new ArrayList<>(n);
Random rand = new Random();
for(int i=0; i<array.size(); i++)
{
System.out.println(i);
array.add(rand.nextInt(100));
}
return array;
}
// print ArrayList
public static void printInt(ArrayList<Integer> a)
{
for (Integer element : a) {
System.out.print(element + " ");
}
System.out.println(" ");
}
public static void main(String[] args)
{
ArrayList<Integer> intArray = fillArray(SIZE);
System.out.println("Initial Array: ");
printInt(intArray);
}
} Function not filling ArrayList and/or Function not printing ArrayList. Program runs fine, but array not in output.
Please Help:
import java.util.Random;
import java.util.ArrayList;
public class StackStuff<T>{
static int SIZE = 10;
public static ArrayList<Integer> fillArray(int n){
ArrayList<Integer> array = new ArrayList<>(n);
Random rand = new Random();
for(int i=0; i<array.size(); i++)
{
System.out.println(i);
array.add(rand.nextInt(100));
}
return array;
}
// print ArrayList
public static void printInt(ArrayList<Integer> a)
{
for (Integer element : a) {
System.out.print(element + " ");
}
System.out.println(" ");
}
public static void main(String[] args)
{
ArrayList<Integer> intArray = fillArray(SIZE);
System.out.println("Initial Array: ");
printInt(intArray);
}
}
Explanation / Answer
import java.util.Random; import java.util.ArrayList; import java.util.Stack; import java.text.DecimalFormat; public class StackStuff{ static int SIZE = 10; public static ArrayList fillArray(int n){ ArrayList array = new ArrayList(n); Random rand = new Random(); for(int i=0; iRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.