E
evrdssadfasdv
Gast
Hallo zusammen =)
ich hoffe ihr könnt mir helfen.
Ich muss einen einfachen java code schreiben, der ein Array befüllt und Elemente wieder heraus löscht und wenn das Array zu klein wird ein neues Anlegt. Das alles ist kein Problem. Wenn ich Test-weiße eine Main einbaue funktioniert alles (vor allem das "vergrößern" des Arrays). So nun habe ich eine jUnit test datei angelegt. Nur hier funktioniert das vergrößern plötzlich nicht mehr.
Code
jUnit
ich hoffe ihr könnt mir helfen.
Ich muss einen einfachen java code schreiben, der ein Array befüllt und Elemente wieder heraus löscht und wenn das Array zu klein wird ein neues Anlegt. Das alles ist kein Problem. Wenn ich Test-weiße eine Main einbaue funktioniert alles (vor allem das "vergrößern" des Arrays). So nun habe ich eine jUnit test datei angelegt. Nur hier funktioniert das vergrößern plötzlich nicht mehr.
Code
Java:
package Übung2;
public class StringStack {
static String[] Array1;
static int ia = 0;
public StringStack(int initialCapacity)
{
Array1 = new String[initialCapacity];
}
/*
public static void main(String[] args)
{
String sa = "Hello";
StringStack TestObjekt = new StringStack(0);
TestObjekt.push(sa);
System.out.println(Array1[0]);
System.out.println(ia);
TestObjekt.push(sa);
System.out.println(Array1[1]);
System.out.println(ia);
TestObjekt.push(sa);
System.out.println(Array1[2]);
System.out.println(ia);
}*/
/**
* Adds a new element to the top of the stack
* @param newElement the element to add
*
*/
public void push(String aString)
{
if(ia == Array1.length)
{
String[] Save = (String[]) Array1.clone();
Array1 = new String[ia+1];
System.arraycopy(Save, 0, Array1, 0, ia);
}
Array1[ia] = aString;
ia++;
}
/**
* Removes and returns the first element of the stack
* @return the element that was the first of the stack
*
*/
public String pop()
{
String SaveString = Array1[0];
System.arraycopy(Array1, 1, Array1, 0, ia);
ia--;
return SaveString;
}
/**
* Returns but does not remove the first element of the stack
* @return the first element of the stack
*
*/
public String peek()
{
return Array1[0];
}
/**
* Tests if this stack is empty
* @return true if this stack contains no elements
*/
public boolean isEmpty()
{
if (Array1.length == 0)
return true;
else
return false;
}
}
jUnit
Java:
package Übung2;
import static org.junit.Assert.assertEquals;
//import static org.junit.Assert.assertTrue;
import org.junit.Before;
import org.junit.Test;
public class StackTest
{
StringStack TestObjekt;
@Before
public void setUp() throws Exception
{
TestObjekt = new StringStack(2);
}
@Test
public final void testaddElement()
{
TestObjekt.push("hallo");
TestObjekt.push("du");
TestObjekt.push("da");
TestObjekt.push("12");
assertEquals("hallo",TestObjekt.pop());
assertEquals("du",TestObjekt.pop());
assertEquals("da",TestObjekt.pop());
assertEquals("12",TestObjekt.pop());
}
}