Почему мой метод ToString не работает на Java?JAVA

Программисты JAVA общаются здесь
Anonymous
Почему мой метод ToString не работает на Java?

Сообщение Anonymous »

Я нахожусь в процессе написания программы подачи новостей, и я пытаюсь проверить, правильно ли добавляются элементы в массив списков. В своем тестовом жгуте я пытаюсь распечатать содержимое массива после добавления выпускаемого предмета, но когда я запускаю программу, ничего не отображается. Есть ли проблема с моим методом ToString (или иным образом)? Спасибо за помощь. < /p>

public class Feed {

private final int DEFAULT_MAX_ITEMS = 10; // default size of array

/* Attribute declarations */
private String name; // the name of the feed
private String[] list; // the array of items
private int size; // the amount of items in the feed

/**
* Constructor
*/
public Feed(String name){
list = new String[DEFAULT_MAX_ITEMS];
size = 0;
}

/**
* add method adds an item to the list
* @param item
*/
public void add(String item){
item = new String();

// add it to the array of items
// if array is not big enough, double its capacity automatically
if (size == list.length)
expandCapacity();

// add reference to item at first free spot in array
list[size] = item;
size++;
}

/**
* expandCapacity method is a helper method
* that creates a new array to store items with twice the capacity
* of the existing one
*/
private void expandCapacity(){
String[] largerList = new String[list.length * 2];
for (int i = 0; i < list.length; i++)
largerList = list;

list = largerList;
}

/**
* toString method returns a string representation of all items in the list
* @return
*/
public String toString(){
String s = "";
for (int i = 0; i < size; i++){
s = s + list.toString()+ "\n";
}
return s;
}

/**
* test harness
*/

public static void main(String args[]) {
Feed testFeed = new Feed("test");
testFeed.add("blah blah blah");
System.out.println(testFeed.toString());
}
< /code>

} < /p>

Подробнее здесь: https://stackoverflow.com/questions/213 ... ng-in-java

Вернуться в «JAVA»