【问题标题】:Can array's index be negative number in Java?Java中数组的索引可以是负数吗?
【发布时间】:2019-07-25 08:21:21
【问题描述】:
public static void main(String[] args) {
    int[] a = new int[10];
    a[1] =2;
    a[-1] = -2;
    a[0] = 0;
    System.out.println(a[-1]);

}

我上面写的代码,编辑器没有判断它是非法的,但是当我运行它时,它给了我 “线程“主”java.lang.ArrayIndexOutOfBoundsException 中的异常:-1”

那么java中的负数索引是否一直都是非法的?或者有什么特殊情况可以让数组的索引为负数?

【问题讨论】:

  • 没有。 Java 数组不能有负索引
  • 然后使用Map<Integer, Integer>
  • 编辑只是帮手,不是评判者。编译器是评判者:)
  • 到目前为止,有八个答案,说的是同一件事。有多少种方式可以说不?
  • 不,负索引是不可能的,但如果你告诉确切的问题你为什么需要这个,你的问题就可以解决。

标签: java arrays


【解决方案1】:

让我们看看 Oracle 的文档here。它是这样说的:

一个数组对象包含许多变量。变量的数量可能为零,在这种情况下,数组被称为空。数组中包含的变量没有名称;相反,它们被使用 non-negative 整数索引值的数组访问表达式引用。这些变量称为数组的组件。如果一个数组有 n 个分量,我们说 n 是数组的长度;使用从 0 到 n - 1(含)的整数索引来引用数组的组件。

还有,

在运行时检查所有数组访问;尝试使用索引 小于零或大于或等于 数组导致抛出 ArrayIndexOutOfBoundsException。

要回答这个问题,不,索引不能为负数。

【讨论】:

    【解决方案2】:

    Java 中不支持负索引。 最后一个元素可以使用

    访问
    array[array.length-1] = lastElement;
    

    【讨论】:

      【解决方案3】:

      那么java中的负数索引是否一直都是非法的?

      是的。

      或者有什么特殊情况可以让数组的索引为负数?

      您可以编写自己的类似数组的集合类型,其中负索引是有效的。它会是一个特定领域的类型,因为我想不出任何一般的应用程序。

      一个简单的例子是

      // define an anonymous class that overrides ArrayList's get behaviour
      List<String> list = new ArrayList<String>() {
          @Override
          public String get(int index) {
              return super.get(Math.abs(index));
          }
      };
      
      list.add("a");
      list.add("b");
      
      System.out.println(list.get(-1)); // b
      System.out.println(list.get(1));  // b
      

      我不建议你这样做。对于任何 Java 开发人员来说,这都是令人困惑和不寻常的。

      【讨论】:

        【解决方案4】:

        简短回答:不。

        简短的回答:数组索引的范围从 0 到数组长度减 1。

        例如:

        int[] ints = new int[5];
        ints[0] = 1;  // fine
        ints[4] = 1;  // fine
        ints[-1] = 1;  // ArrayIndexOutOfBoundsException thrown
        ints[5] = 1;  // ArrayIndexOutOfBoundsException thrown
        

        【讨论】:

          【解决方案5】:

          简而言之,不,它们不能为负数。

          来自docs(例如使用get)

          ArrayIndexOutOfBoundsException - 如果指定的索引参数是负数,或者如果它大于或等于指定数组的长度

          【讨论】:

            【解决方案6】:

            在java中,不能使用负索引。它是基于 0 的正指数。如果使用负索引,那么 java 将抛出 "ArrayIndexOutOfBoundsException" 异常。

            【讨论】:

              【解决方案7】:

              java中的数组不能用负数索引,“问题”是java中到目前为止还没有定义这样的无符号整数,所以只要x是一个有效的数字索引,写myArr[x]就会编译

              p>

              【讨论】:

                【解决方案8】:

                正如@Darshan Mehta 的回答所说,负数不允许作为数组索引。但是,您始终可以编写自己的允许负索引的数组类。

                这是我使用泛型的示例。

                public class ModuloArray<E> {
                    int size;
                    E[] array;
                
                    public ModuloArray(int size) {
                        if (size <= 0)
                            throw new RuntimeException("not positive");
                        
                        this.size = size;
                        array = (E[]) new Object[size];
                    }
                    
                    public ModuloArray(E... elements) {
                        if (elements == null)
                            throw new RuntimeException("null");
                        
                        this.size = elements.length;
                        array = elements;
                    }
                
                    public E get(int index) {
                        return array[Math.floorMod(index, size)];
                    }
                    
                    public void set(int index, E element) {
                        array[Math.floorMod(index, size)] = element;
                    }
                }
                
                public class Main {
                    public static void main(String[] args) {
                        System.out.println("*** ModuloArray with first constructor ***");
                        ModuloArray<String> myArray = new ModuloArray<>(3);
                        myArray.set(-1, "apple");
                        myArray.set(-2, "orange");
                        myArray.set(-3, "banana");
                        
                        System.out.println(myArray.get(-3));
                        System.out.println(myArray.get(-2));
                        System.out.println(myArray.get(-1));
                        System.out.println(myArray.get(0));
                        System.out.println(myArray.get(1));
                        System.out.println(myArray.get(2));
                        
                        System.out.println();
                        System.out.println("*** ModuloArray with second constructor ***");
                        ModuloArray<String> myArray2 = new ModuloArray<>("glazed donut", "chicken burger", "apple soda");
                        
                        System.out.println(myArray2.get(-2));
                        System.out.println(myArray2.get(3));
                    }
                }
                
                

                上面的例子输出:

                *** ModuloArray with first constructor ***
                banana
                orange
                apple
                banana
                orange
                apple
                
                *** ModuloArray with second constructor ***
                chicken burger
                glazed donut
                

                Java 中的数组索引只有在0 &lt;= index &lt; array.length 为真时才有效。但是,因为ModuloArray 使用Math.floorMod(int x, int y) 来访问数组元素,所以任何整数(包括负数) 都可以用作有效索引。这种行为类似于 Python 中列表的索引工作方式。

                【讨论】:

                  猜你喜欢
                  • 2014-01-13
                  • 2011-06-19
                  • 2011-05-02
                  • 1970-01-01
                  • 2014-01-21
                  • 1970-01-01
                  • 2020-02-03
                  • 1970-01-01
                  相关资源
                  最近更新 更多