【问题标题】:How to implement negative indexes in java? [closed]如何在java中实现负索引? [关闭]
【发布时间】:2015-05-02 08:28:27
【问题描述】:

在 Python 中,您可以使用负数组索引从数组的右侧开始计数。例如,array[-1] 是最后一个元素,array[-2] 是数组中倒数第二个元素。你将如何在 Java 中实现这一点?

【问题讨论】:

  • 现在更容易理解所询问的内容。有人可以重新提出这个问题吗?
  • 当然你可以创建你自己的对象。但它会有一个类似 PythonArray.getElement(int elementIndex) 的方法
  • 接受正数和负数索引的单行代码如下所示:i >= 0 ? array[i] : array[array.length+i]; 您可以将其作为静态方法添加到 Utils 类中,然后调用该方法,例如 Util.getElementAt(array, i); , 而不是 array[i].
  • 你可以这样模拟:int idx = (array.length + negIdx) % array.length;
  • 我发布了一个关于负数组索引here 的示例代码的答案。不幸的是,这个问题已经结束,所以我不能把它留在这里,但我相信它很好地回答了这个问题。

标签: java arrays


【解决方案1】:

Java 不支持负索引,要访问最后一个单元格,您应该使用

array[array.length-1] = lastElement;

【讨论】:

    【解决方案2】:

    Java 下标索引从 0 开始。不能使用负索引。如果完全使用,那么 java 将抛出 Array Index out of bounds 异常。

    【讨论】:

      【解决方案3】:

      要实现这样的功能,您必须创建一个循环的双向链表... 我没有编译和测试这个,但这是大致的想法......

      public class LinkedList {
          Integer node;
          LinkedList next;
          LinkedList prev;
          public LinkList(Integer node) {
              this.node = node;
              this.next = this;
              this.prev = this;
          }
          public void insert(Integer node) {
              if(this.node == null) {
                  this.node = node;
                  this.next = this;
                  this.prev = this;
              }
              else if(this.next == null) {
                  this.next = new LinkedList(node);
                  this.prev = node
                  this.next.prev = this;
                  this.next.next = this;
              }
              else {
                  this.next(node, this);
              }
          }
          private void insert(Integer node, LinkedList head) {
              if(this.next == null) {
                  this.next = new LinkedList(node);
                  this.next.prev = this;
                  this.next.next = head;
              }
              else {
                  this.next(node, head);
              }
          }
          public Interger get(int index) {
              int cursor = 0;
              if(index == cursor) {
                  return this.node;
              }
              else if(index < cursor) {
                  return this.prev.get(index, cursor-1);
              }
              else {
                  return this.next.get(index, cursor+1);
              }
          }
          private Interger get(int index, int cursor) {
              if(index == cursor) {
                  return this.node;
              }
              else if(index < cursor) {
                  return this.prev.get(index, cursor-1);
              }
              else {
                  return this.next.get(index, cursor+1);
              }
          }
      }
      public static void main(String[] args) {
          LinkedList list = new LinkedList(new Integer(1));
          list.insert(new Integer(2));
          list.insert(new Integer(3));
          System.out.println(list.get(-1).toString());
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-20
        • 1970-01-01
        • 1970-01-01
        • 2013-02-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多