这在数组中如何工作?

发布于 2024-10-31 05:41:36 字数 1167 浏览 1 评论 0原文

你好,我在编写这段代码时遇到困难,我对最后两种方法迷失了。这是一个学习练习(不是家庭作业),但我需要示例来学习。 另外,我认为这在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

无名指的心愿 2024-11-07 05:41:36
public boolean contains(int number) {
     for (int i=0; i<values.length; i++) 
          if (number==values[i]) return true;
     return false;
}

public void add(int number) {
     int[] tmp = new int[value.length+1];
     for (int i=0; i<values.length; i++) tmp[i] = values[i];
     tmp[tmp.length-1] = number;
     values = tmp;
}
public boolean contains(int number) {
     for (int i=0; i<values.length; i++) 
          if (number==values[i]) return true;
     return false;
}

public void add(int number) {
     int[] tmp = new int[value.length+1];
     for (int i=0; i<values.length; i++) tmp[i] = values[i];
     tmp[tmp.length-1] = number;
     values = tmp;
}
迎风吟唱 2024-11-07 05:41:36

应该如下所示:

public boolean contains(int number) {

     for (int i=0; i<values.length; i++){ 
        //so the number was found
        if (number==values[i]){ 
            return true;

     }
     return false; 
  }

您还可以使用 Arrays.binarySearch() 方法而不是自己编写一个。二分查找会查找该数字是否在数组中以及索引是什么(当然数组必须是有序的)。

Should be something like below:

public boolean contains(int number) {

     for (int i=0; i<values.length; i++){ 
        //so the number was found
        if (number==values[i]){ 
            return true;

     }
     return false; 
  }

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).

染墨丶若流云 2024-11-07 05:41:36

伟大的开始。您需要更改的内容如下:

public boolean contains(int number) {

    for (int i=0; i<values.length; i++) { 
        if (values[i] == number) { 
            return true;
    }

    // Since this line is reached only if no values matched, you simply do...
    return false;
}

由于您仍在学习,我将给您更多的指导作为奖励:-)

  • 我会更改

    if (索引>=values.length)
    

    if (索引 < 0 || 索引 >=values.length)
    
  • 您可以到处使用 for-each 循环。例如,您可以这样写:

    public long getTotal() {
        长和=0;
        for (int i : 值)
            总和=总和+我;
    
        返回总和;
    }
    

Great start. All you needed to change was the following:

public boolean contains(int number) {

    for (int i=0; i<values.length; i++) { 
        if (values[i] == number) { 
            return true;
    }

    // Since this line is reached only if no values matched, you simply do...
    return false;
}

Since you're still learning, I'll give you a few more pointers as a bonus :-)

  • I would change

    if (index >= values.length)
    

    to

    if (index < 0 || index >= values.length)
    
  • You could make use of for-each loops here and there. You could for instance write:

    public long getTotal() {
        long sum = 0;
        for (int i : values)
            sum = sum + i;
    
        return sum;
    }
    
神也荒唐 2024-11-07 05:41:36

其中一些方法可以简化。

public NumberList(int[] a) {
    values = a.clone();
}

public int getAt(int index) {
    return values[index]; // throws ArrayIndexOutOfBoundException if out of bounds. It include the invalid index as well.
}

public long getTotal() {
    long sum = 0;
    for (int i: values) sum += i;
    return sum;
}

public boolean contains(int number) {
    for (int i: values) 
        if (number == i)
            return true;
    return false;
}

public void add(int num) {
    int[] values2 = new int[values.length+1];
    // arraycopy is typically faster than using a loop.
    System.arraycopy(values,0,value2,0,values.length);
    values2[values.length] = num;
    values = values2;
}

Some of these methods can be simplified.

public NumberList(int[] a) {
    values = a.clone();
}

public int getAt(int index) {
    return values[index]; // throws ArrayIndexOutOfBoundException if out of bounds. It include the invalid index as well.
}

public long getTotal() {
    long sum = 0;
    for (int i: values) sum += i;
    return sum;
}

public boolean contains(int number) {
    for (int i: values) 
        if (number == i)
            return true;
    return false;
}

public void add(int num) {
    int[] values2 = new int[values.length+1];
    // arraycopy is typically faster than using a loop.
    System.arraycopy(values,0,value2,0,values.length);
    values2[values.length] = num;
    values = values2;
}
挽袖吟 2024-11-07 05:41:36

第一种方法:

public boolean contains(int number) 
{
    for (int i=0; i<values.length; i++)
    {
        if (number == values[i])
        { 
            return true;
        }
    }

    return false;
}

第二种方法: 为此,您必须维护一个名为 current 的变量,其中包含最近添加的元素的索引。

public void add(int number)
{
    if(current == values.length - 1)
    {
        throw new RuntimeException("Array Overflow");
    }

    values[++current] = number;
}

First method:

public boolean contains(int number) 
{
    for (int i=0; i<values.length; i++)
    {
        if (number == values[i])
        { 
            return true;
        }
    }

    return false;
}

Second Method: For this you have to maintain a variable called current that contains the index of the most recently added element.

public void add(int number)
{
    if(current == values.length - 1)
    {
        throw new RuntimeException("Array Overflow");
    }

    values[++current] = number;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文