From c4b33cd2f64c40410121ea73e309f892194cbe4d Mon Sep 17 00:00:00 2001 From: mvholmsy <mvholmsy@student.jyu.fi> Date: Mon, 2 Mar 2020 10:17:13 +0200 Subject: [PATCH] =?UTF-8?q?J=C3=A4senet=20-tietorakenne=20p=C3=A4ivitetty?= =?UTF-8?q?=20dynaamiseksi,=20eli=20kasvattaa=20itse=C3=A4=C3=A4n?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Tietorakenteet/Jasenet.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/Tietorakenteet/Jasenet.java b/src/Tietorakenteet/Jasenet.java index 5a363f7..eb67eaa 100644 --- a/src/Tietorakenteet/Jasenet.java +++ b/src/Tietorakenteet/Jasenet.java @@ -6,7 +6,7 @@ package Tietorakenteet; * Hiihtorekisterin jasenet, joka lisää ja poistaa jäseniä. */ public class Jasenet { - private static final int MAX_JASENIA = 10; + private static int MAX_JASENIA = 5; private int lkm = 0; private String tiedostonNimi = ""; private Jasen alkiot[] = new Jasen[MAX_JASENIA]; @@ -47,8 +47,13 @@ public class Jasenet { */ public void lisaa(Jasen jasen) throws SailoException { 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; - //jasenisto.add(jasen); lkm++; } @@ -60,6 +65,7 @@ public class Jasenet { * @throws IndexOutOfBoundsException jos i ei ole sallitulla alueella */ public Jasen anna(int i) throws IndexOutOfBoundsException { + if (i < 0 || lkm <= i) throw new IndexOutOfBoundsException("Laiton indeksi: " + i); return alkiot[i]; -- GitLab