Skip to content
Snippets Groups Projects
Commit c4b33cd2 authored by mvholmsy's avatar mvholmsy
Browse files

Jäsenet -tietorakenne päivitetty dynaamiseksi, eli kasvattaa itseään

parent 6bc14f82
No related branches found
No related tags found
No related merge requests found
...@@ -6,7 +6,7 @@ package Tietorakenteet; ...@@ -6,7 +6,7 @@ package Tietorakenteet;
* Hiihtorekisterin jasenet, joka lisää ja poistaa jäseniä. * Hiihtorekisterin jasenet, joka lisää ja poistaa jäseniä.
*/ */
public class Jasenet { public class Jasenet {
private static final int MAX_JASENIA = 10; private static int MAX_JASENIA = 5;
private int lkm = 0; private int lkm = 0;
private String tiedostonNimi = ""; private String tiedostonNimi = "";
private Jasen alkiot[] = new Jasen[MAX_JASENIA]; private Jasen alkiot[] = new Jasen[MAX_JASENIA];
...@@ -47,8 +47,13 @@ public class Jasenet { ...@@ -47,8 +47,13 @@ public class Jasenet {
*/ */
public void lisaa(Jasen jasen) throws SailoException { public void lisaa(Jasen jasen) throws SailoException {
if (lkm >= alkiot.length) throw new SailoException("Liikaa alkioita"); if (lkm >= alkiot.length) throw new SailoException("Liikaa alkioita");
// TODO jos taulukko on täynnä, tehdään uusi taulukko,
Jasen uusi[] = new Jasen[MAX_JASENIA+5];
for (int i = 0; i < alkiot.length; i++) {
uusi[i] = alkiot[i];
}
alkiot = uusi;
alkiot[lkm] = jasen; alkiot[lkm] = jasen;
//jasenisto.add(jasen);
lkm++; lkm++;
} }
...@@ -60,6 +65,7 @@ public class Jasenet { ...@@ -60,6 +65,7 @@ public class Jasenet {
* @throws IndexOutOfBoundsException jos i ei ole sallitulla alueella * @throws IndexOutOfBoundsException jos i ei ole sallitulla alueella
*/ */
public Jasen anna(int i) throws IndexOutOfBoundsException { public Jasen anna(int i) throws IndexOutOfBoundsException {
if (i < 0 || lkm <= i) if (i < 0 || lkm <= i)
throw new IndexOutOfBoundsException("Laiton indeksi: " + i); throw new IndexOutOfBoundsException("Laiton indeksi: " + i);
return alkiot[i]; return alkiot[i];
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment