这在数组中如何工作?
你好,我在编写这段代码时遇到困难,我对最后两种方法迷失了。这是一个学习练习(不是家庭作业),但我需要示例来学习。 另外,我认为这在 stackoverflow 数据库中也很有用。
public class NumberList {
public int[] values;
public NumberList() {
values = new int[0];
}
public NumberList(int[] a) {
values = new int [a.length];
for (int i=0;i<a.length;i++)
values[i] = a[i];
}
public int getSize() {
return this.values.length;
}
public int getAt(int index) {
if (index>=values.length){
throw new IndexOutOfBoundsException ("Values of out of bounds");
}else{
return values[index];
}
}
public long getTotal() {
long sum = 0;
for (int i=0; i<values.length; i++) {
sum = sum + values[i];
}
return sum;
}
// need help here its a boolean that the number is in the array but if not its //false
public boolean contains(int number) {
for (int i=0; i<values.length; i++){
if (number <values.length+1){
return true;
}
//else
// return false;
// }
// this is an add method that confuses me and ask myself why since i added without it.
public void add(int number) {
number=0;
}
}
hello I am having difficulty writing this code, I am lost on the last two methods. This is a learning exercise (not homework), but I need examples to learn.
Plus I thought this would be useful in the stackoverflow database as well.
public class NumberList {
public int[] values;
public NumberList() {
values = new int[0];
}
public NumberList(int[] a) {
values = new int [a.length];
for (int i=0;i<a.length;i++)
values[i] = a[i];
}
public int getSize() {
return this.values.length;
}
public int getAt(int index) {
if (index>=values.length){
throw new IndexOutOfBoundsException ("Values of out of bounds");
}else{
return values[index];
}
}
public long getTotal() {
long sum = 0;
for (int i=0; i<values.length; i++) {
sum = sum + values[i];
}
return sum;
}
// need help here its a boolean that the number is in the array but if not its //false
public boolean contains(int number) {
for (int i=0; i<values.length; i++){
if (number <values.length+1){
return true;
}
//else
// return false;
// }
// this is an add method that confuses me and ask myself why since i added without it.
public void add(int number) {
number=0;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
应该如下所示:
您还可以使用 Arrays.binarySearch() 方法而不是自己编写一个。二分查找会查找该数字是否在数组中以及索引是什么(当然数组必须是有序的)。
Should be something like below:
You can also use Arrays.binarySearch() method instead of writing one yourself. The binary search will find if the number is in the array and what index (of course the array has to be ordered).
伟大的开始。您需要更改的内容如下:
由于您仍在学习,我将给您更多的指导作为奖励:-)
我会更改
至
Great start. All you needed to change was the following:
Since you're still learning, I'll give you a few more pointers as a bonus :-)
I would change
to
You could make use of for-each loops here and there. You could for instance write:
其中一些方法可以简化。
Some of these methods can be simplified.
第一种方法:
第二种方法: 为此,您必须维护一个名为
current
的变量,其中包含最近添加的元素的索引。First method:
Second Method: For this you have to maintain a variable called
current
that contains the index of the most recently added element.