Hello,
How to put or connect list from getListaLiczbCalkowitych to getMinimum?
Some people write somthing like this:
int min=array.get(0);
for(int b=1;b<array.size();b++) {
if(array.get(b)<min){
min=array.get(b);
}
}
return min;
They use array.get() but how it know is the array is from getListaLiczbCalkowitych ?
I tried write like this, but i don't know it is correct. It finally didn't worked.
ArrayList<Integer> lista2 = new ArrayList<>(lista);
Can someone explain me this a little? :)
package pl.codegym.task.task08.task0822;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
/*
Najmniejsza z N liczb
*/
public class Solution {
public static void main(String[] args) throws Exception {
List<Integer> listaLiczbCalkowitych = getListaLiczbCalkowitych();
System.out.println(getMinimum(listaLiczbCalkowitych));
}
public static int getMinimum(List<Integer> array) {
// Znajduje minimum
ArrayList<Integer> lista = new ArrayList<>();
int min = lista.get(0);
for(int i=1; i < lista.size(); i++) {
if(lista.get(i) < min) {min = lista.get(i);}
}
//System.out.println("Najmniejsza wartość: " + min); // dispaly check min
return min;
}
public static List<Integer> getListaLiczbCalkowitych() throws IOException {
// Tworzy i inicjalizuje listę
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
ArrayList<Integer> lista = new ArrayList<>(); // I'm initialize the list.
String a = reader.readLine(); // I enter the number N from the keyboard (req 4.)
int n = Integer.parseInt(a); // ...
for (int i = 0; i < n; i++) { // then I enter N numbers, and add it to a list (req 4.)
String b = reader.readLine();
int x = Integer.parseInt(b);
lista.add(x);
}
return lista; // I'm returning a list with N numbers (req 4.)
}
}