【问题标题】:how does this work in arrays?这在数组中如何工作?
【发布时间】:2011-04-07 21:16:20
【问题描述】:

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



}

}

【问题讨论】:

  • 你实际上并没有问任何问题。问题是添加方法吗?应该怎么做,给数组添加新索引?
  • 您能否给出一组输入以及它们的预期输出是什么?
  • 您不需要检查数组边界,Java 会为您执行此操作并引发相同的异常,除非您可能想要您的消息而不是默认消息。
  • 看看System.arrayCopy()
  • 其实这个好像是这个的副本:stackoverflow.com/questions/5573534/…

标签: java arrays methods


【解决方案1】:
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;
}

【讨论】:

    【解决方案2】:

    应该如下所示:

    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() 方法而不是自己编写一个。二分查找会查找数字是否在数组中以及索引是什么(当然数组必须是有序的)。

    【讨论】:

      【解决方案3】:

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

      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 (index >= values.length)
        

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

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

      【讨论】:

      • 你有什么建议吗?还是值得学习的好书?我去了一所非常难的顶级计算机科学学校,我正在尽我最大的努力生存下去。我每天学习 18 小时,对学习有任何帮助,我非常感激!
      • 这不是假设数组只包含递增的数字,并且每个数字都从 1 到 length 吗?例如,如果数组是 {1, 3, 5} 并且您传入 contains(2) 将返回 true,但它实际上是 false
      • 对不起。我的错。更新了答案。
      • 如果该方法旨在检查数组是否包含该索引的值怎么办?如果不了解代码的更多信息,很难判断哪里出了问题。
      【解决方案4】:

      其中一些方法可以简化。

      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;
      }
      

      【讨论】:

        【解决方案5】:

        第一种方法:

        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;
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-07-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-05-28
          相关资源
          最近更新 更多