在指定的索引处添加单个元素。

huangapple go评论109阅读模式
英文:

Add a single element at the specified index

问题

I'd love to exercise the CRUD principle in ArrayList in Java.
I started today by exercising the add method; however, I get an error when I add the method add in the static main method. I get an error that the add method should be static, and when I make it static, I get an error with the ArrayList and Scanner as strange elements, even when I add those to the static add method. After executing the IDE, I get the error "Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 2, Size: 0." Could someone explain to me why this is so?

package com.company;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {

    static Scanner scanner = new Scanner(System.in);
    static List<String> arrayList1 = new ArrayList<>();

    public static void main(String[] args) {

        add();
        System.out.println(arrayList1);
    }

    public static void add() {
        int index = scanner.nextInt();
        String element = scanner.next();
        arrayList1.add(index, element);
    }
}
英文:

I'd love to exercise the CRUD principle in ArrayList in java
I started today by exercising the add method however I get error
when I add the methode add in static main methode I get error that the add methode should be static
and when I put it static I get error by the arraylist and scanner as Strange element even wenn I add those the the static add methode After excuting the ide I get error " Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 2, Size: 0" could someone explain me why is so

package com.company;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {

    Scanner scanner = new Scanner(System.in);
    List&lt;String&gt; arrayList1 = new ArrayList&lt;&gt;();

    public static void main(String[] args) {
	
        Scanner scanner = new Scanner(System.in);
        List&lt;String&gt; arrayList1 = new ArrayList&lt;&gt;();

        add();
        System.out.println(arrayList1);
    }

    public static void add(){
        Scanner scanner = new Scanner(System.in);
        List&lt;String&gt; arrayList1 = new ArrayList&lt;&gt;();

        int index=scanner.nextInt();
        String element=scanner.next();
        arrayList1.add(index,element);

    }
}

答案1

得分: 2

简而言之:你不能在未初始化的列表中的特定索引(不是第0个索引)处添加元素。当你调用添加函数时,数组列表为空,这就是为什么你会收到IndexOutOfBoundsException错误。

英文:

To put it simply: You can't just add an element at a certain index (that isn't the 0th) in an uninitialized list. When you call your add function the array list is empty, which is why you're getting an IndexOutOfBoundsException error.

答案2

得分: 1

当你创建一个列表时,它最初是空的。如果在索引0没有项目,你就无法向索引2添加元素。ArrayList类不会在位置0和1创建元素。

你总是可以向列表的末尾或开头添加元素,即使列表是空的。这总是安全的:

arrayList1.add(0, element); // 添加到列表的开头
arrayList1.add(element);    // 添加到列表的末尾

你的程序有一些重复和未使用的变量。例如,scannerarrayList1 不必在每个方法中重新声明和初始化 - 实际上,这样做会导致后续出现错误。你可以将类级别的变量声明为static,然后可以在所有方法中使用它们:

class Main {
    static Scanner scanner = new Scanner(System.in);
    static List<String> arrayList1 = new ArrayList<>();

    public static void main(String[] args) {
        add();
        System.out.println(arrayList1);
    }

    public static void add() {
        int index = scanner.nextInt();
        String element = scanner.next();
        arrayList1.add(index, element);
    }
}

这不是在Java中编程的最佳方式,当你的学习引导你到面向对象的概念时,你会了解到更好的方式,但对于初学者来说,这已经足够了。

英文:

When you create a list, it is initially empty. You can't add an element to index 2 if there is no item at index 0. The ArrayList class does not create elements out of nowhere for the positions 0 and 1.

You can always add an element to the end of a list, or to the beginning of a list, even if the list is empty. These are always safe:

arrayList1.add(0, element); // Add to beginning of the list
arrayList1.add(element);    // Add to end of the list

Your program has some duplicate and unused variables. For example, scanner and arrayList1 don't have to re-declared and initialized in every method - in fact, doing it will lead to bugs further down the line. You can make the class-level variables static and then you can use them in all methods:

class Main {
    static Scanner scanner = new Scanner(System.in);
    static List&lt;String&gt; arrayList1 = new ArrayList&lt;&gt;();

    public static void main(String[] args) {
        add();
        System.out.println(arrayList1);
    }

    public static void add() {
        int index = scanner.nextInt();
        String element = scanner.next();
        arrayList1.add(index, element);
    }
}

This is not the best way to program in Java, as you will learn when your studies bring you to object oriented concepts but for a beginner this is good enough

huangapple
  • 本文由 发表于 2020年8月20日 02:20:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/63492839.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定