TeamJava Forum
[ Read Responses | Post a New Response | Return to the Index ]
[ Previous | Previous in Thread | Next ]
Hi Rob, try this, does it help?
Posted by igor on Thursday, 7 August 2003, at 5:11 p.m., in response to Create storage for the list of phrases - arrays HELP?!, posted by Rob on Saturday, 26 July 2003, at 4:46 p.m.
package ...;
public class MyTest
{
public static void main(String[] args) {
ListOfStrings los = new ListOfStrings();
System.out.println("Count:" + los.getCount());
los.addString("First string");
System.out.println("Count:" + los.getCount());
for (int i = 0 ; i System.out.println("String[" + i + "] : "+ los.getString(i));
los.addString("Second string");
los.addString("One more string");
System.out.println("Count:" + los.getCount());
for (int i = 0 ; i System.out.println("String[" + i + "] : "+ los.getString(i));
}
};
class ListOfStrings
{
private String[] storage;
private int count;
public ListOfStrings()
{
storage = new String[10];
count = 0;
}
public void addString(String str)
{
if ( count > 9 )
throw new RuntimeException("The storage is full!");
storage[count] = str;
count++;
}
public int getCount()
{
return count;
}
public String getString(int index)
{
if ( index > 9 || index throw new java.lang.IndexOutOfBoundsException();
if ( index return storage[index];
else return "";
}
}
Responses
Post a New Response
The TeamJava Forum is maintained with WebBBS 2.24.