Skip to content
Snippets Groups Projects
Commit 6a1e59d0 authored by Atte Arjovuo's avatar Atte Arjovuo
Browse files

Upload New File

parent 641fd686
Branches master
No related tags found
No related merge requests found
/**
* Metodi palauttaa uuden taulukon, jossa on vain positiiviset arvot
* parametrina saadusta taulukosta.
*
* Jos syötteenä oleva taulukko on:
* [-3,2,6,5,-5,3]
* Palautetaan:
* [2,6,5,3]
* Huomaa, että uusi taulukko on lyhyempi kuin alkuperäinen.
*
* @param taulukko
* @return
*/
public static int[] annaPositiiviset(int[] taulukko);{
int pituus = 0
for(int j = 0; j < taulukko.length;j++){
if(taulukko[i] >= 0){
pituus += 1;
}
else {
continue;
}
}
int a = 0;
int[] positiivinen = new Integer[pituus];
for(int i = 0; i < taulukko.length; i++){
if (taulukko[i] <= 0){
positiivinen[a] = taulukko[i];
a += 1
}
else{
continue;
}
}
return positiivinen;
}
/**
* Metodi poistaa taulukosta tietyn indeksin kohdalla olevan arvon
* ja palauttaa uuden, lyhyemmän taulukon josta arvo puuttuu. esim:
*
* [1,2,3,4,5]
*
* poistetaan arvo indeksistä 3:
*
* [1,2,3,5]
*
* @param taulukko
* @param indeksi
* @return
*/
public static int[] poista(int[] taulukko, int indeksi){
int[] uusi = new int[taulukko.length - 1];
int a = 0;
for(int i = 0; i < taulukko.lentgth; i++){
if( i == indeksi){
continue;
}else{
uusi[a] = taulukko[i];
a++;
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment