参与分配与创建方法 [英] Assignment involved with creating methods
问题描述
首先感谢你在看我的问题。我的任务是pretty基本大部分,但我是一个没有经验的程序员。下面是分配的描述:
first of all thank you for looking at my question. My assignment is pretty basic to most but I am an inexperienced programmer. Here is the description of the assignment:
你的目标是写一个类名为Storage,可容纳字符串的集合。它必须实现以下方法:
Your objective is to write a class called Storage that can hold a collection of Strings. It must implement the following methods:
- 的addItem(String s)将 - 此方法接受一个字符串参数,并返回一个布尔值。参数引用然后将其存储在您选择的结构。如果成功添加了引用该方法返回true。
- getItems() - 此方法不带参数,返回一个包含存储在内部结构中的所有字符串数组。返回的数组不能包含任何null元素。
- isFull() - 这个方法,如果没有内部存储可返回true,否则为false
- 的removeItem(String s)将 - 此方法接受一个字符串参数,并返回一个布尔值。该方法必须通过内部存储结构搜索和删除参数引用(如present)。如果引用是成功删除的方法返回true,否则为false。
好的家伙,我做了一些改变,我开始有些code,但它不会正确编译。基于这些方法的描述可有人告诉我什么,我做错了吗?
I am completely stumped on most of these. I am using an array to store the data, but I am open to other suggestions. Thanks again.
Alright guys I made a few changes and I started some code, but it won't compile correctly. Based on the description of the methods can someone tell me what I'm doing wrong?
公共类存储{
//variables
private String[] list;
private int size = 10;
private int index = 0;
public boolean addItem(String s) {
for (int i = 0; i < list.length; i++) {
if (!list.equals(null)) {
list[i] = s;
}
}
return true;
}
public String[] getItems() {
for (int i = 0; i < list.length; i++) {
if(!list.equals(null)) {
System.out.println(list[i]);
i++;
}
}
return list;
}
public boolean isFull() {
if (list.length > size) {
System.out.println("The array is full");
}
return true;
}
public boolean removeItem(String s) {
for (int i = index; i < list.length - 1; i++) {
list[i] = list[i + 1];
}
return true;
}
}
推荐答案
的addItem(String s)将
:该方法应该返回一个布尔值,因为您使用的是的ArrayList
,我们可以使用添加(对象o)
,它返回一个布尔值。
addItem(String s)
: The method should return a boolean value, since you are using an ArrayList
, we can use add(Object o)
, which returns a boolean.
public boolean addItem(String s) {
return list.add(s); // I'm assuming that your instance variable is named list
}
getItems()
:使用方法叫做的ArrayList
类的toArray()
,传递数组类型的参数转换为。
getItems()
: Use the method in the ArrayList
class called toArray()
, passing in an argument of the array type to cast to.
public String[] getItems() {
return list.toArray(new String[list.size()]);
}
isFull()
:不完全知道你的老师是问什么在这里,作为一个的ArrayList
将自动调整,但你可以只添加一个实例变量到你的类,它包含一个最大尺寸,然后对证像这样...
isFull()
: Not exactly sure what your instructor was asking for here, as an ArrayList
will automatically resize, but you could just add an instance variable to your class that contains a maximum size and then check against it like so...
public static final int SIZE = 10;
...
public boolean isFull() {
return list.size() >= SIZE;
}
当然,这意味着将code检查您要添加元素不违反能力。我会在下面的完整的例子表明这一点。
of course, this means adding code to check that the element you're adding doesn't breach capacity. I'll show this in the complete example below.
的removeItem(String s)将
:在的ArrayList 的
删除()
方法code>超载接受对象删除。同样,这种方法也返回一个布尔值。
removeItem(String s)
: The remove()
method in ArrayList
is overloaded to accept an object to remove. Likewise, this method also returns a boolean value.
public boolean removeValue(String s) {
return list.remove(s); // Removes the first occurrence of the string
}
所有的一切,你应该有类似下面的东西...
All in all, you should have something similar to the following...
public class Storage {
private ArrayList<String> list = new ArrayList<>();
public static final int SIZE = 10;
public boolean addItem(String s) {
if (isFull())
return false;
return list.add(s);
}
public String[] getItems() {
return list.toArray(new String[list.size()]);
}
public boolean isFull() {
return list.size() >= SIZE;
}
public boolean removeValue(String s) {
return list.remove(s); // Removes the first occurrence of the string
}
}
这篇关于参与分配与创建方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!