ArrayList <Integer> as parameter ...?

I would like to know how to create a method that takes an ArrayList from integers (ArrayList) as a parameter and then displays the contents of an ArrayList?

I have some code that generates some random numbers and populates an ArrayList with results, however when I try to create this particular method I save errors in the eclipse.

Here is what I still have:

public void showArray(ArrayList<Integer> array){

    return;

}

I know that it is very simple, but I don’t know exactly how to approach it - could it be something like the following?

public void showArray(ArrayList<Integer> array){

    Arrays.toString(array);

}

Any help would be greatly appreciated.

Thank.

+3
source share
4 answers

I assume this is a training exercise. I will give you some tips:

  • showArray, ArrayList<T> List<T> . , , . , .
  • , , , , .
  • . , String .

- :

public void printList(List<Integer> array) {
    String toPrint = ...;
    System.out.println(toPrint);
}

StringBuilder toPrint.

+7

, System.out.println( array ); ?

:

[1, 2, 3]
+1

,

public void showArray(ArrayList<Integer> array){

 for(int arrayItem : array)
 {
    System.out.println(arrayItem);
 }

}
0

Looks like someone wants us to do our homework. You do not need to return anything if you just show it, and if the method has a return type of void. I don’t know exactly what you want, but is it something similar to System.out.println (array.elementAt (index))? then you need a loop.

-2
source

Source: https://habr.com/ru/post/1796403/


All Articles