首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >是否可能不正确调用ArrayList索引?

是否可能不正确调用ArrayList索引?
EN

Stack Overflow用户
提问于 2015-04-20 19:08:57
回答 3查看 106关注 0票数 1

所以这个程序的目标是创建一个方法,它将一个字符串和一个ArrayList作为参数,它将字符串添加到ArrayList中,然后按字母顺序对整个程序进行字母化,并返回字母化的ArrayList,包括字符串。

我遇到麻烦的是alphabetized.get(x) = arr.get(x);

程序不会编译,因为它不识别值x,但我在前面的for循环中说明了它,所以我不太确定它为什么不能运行.

代码语言:javascript
复制
public class TestArrays {
    static Scanner reader = new Scanner(System.in);

    public static void main(String[] args) {    
        String pat;

        ArrayList<String> names = new ArrayList<String>();
        names.add("anna");
        names.add("bob");
        names.add("matthew");
        names.add("charles");
        names.add("effron");
        System.out.print(newArray(names, pat));
    }

    public static ArrayList<String> newArray(ArrayList<String> arr, String str) {
        List<String> alphabetized = new ArrayList<String>(arr.size());
        arr.add(str);
        java.util.Collections.sort(arr);
        for (int x=0; x<=arr.size();x++){
            alphabetized.get(x) = arr.get(x);
        }
        return alphabetized;
    }
}

谢谢

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-04-20 19:35:38

几件事。

首先,在newArray方法中,不需要创建副本并返回该副本,除非您不想更改原始的ArrayList

代码语言:javascript
复制
static Scanner reader = new Scanner(System.in);

public static void main(String[] args) {    
    String pat = reader.nextLine();

    ArrayList<String> names = new ArrayList<String>();
    names.add("anna");
    names.add("bob");
    names.add("matthew");
    names.add("charles");
    names.add("effron");
    System.out.print(newArray(names, pat));
}

public static ArrayList<String> newArray(ArrayList<String> arr, String str) {
    ArrayList<String> alphabetized = new ArrayList<String>(arr.size());
    arr.add(str);
    java.util.Collections.sort(arr);

    return arr;
    //for (int x=0; x<arr.size();x++){
    //    alphabetized.add(arr.get(x));
    //}
    //return alphabetized;
}

结果:

第二,如果要复制到另一个数组,请理解这一行代码。

代码语言:javascript
复制
    ArrayList<String> alphabetized = new ArrayList<String>(arr.size());

您刚刚创建了一个列表,它的容量与传入的数组大小相同,其中没有实际元素。您仍然必须添加到列表中,就像其他列表一样。一旦添加了使大小大于容量的元素,就会自动分配新内存来保存新项。

编辑

我忘了提到用于复制的for循环应该是

代码语言:javascript
复制
for (int x = 0; x < arr.size(); x++)

而不是

代码语言:javascript
复制
for (int x = 0; x <= arr.size(); i++)

您的原始for循环将导致IndexOutOfBounds异常,因为您将试图从原始数组中获取超过其最大索引值的项。一个10项列表的大小为10,但索引为0- 9。您原来的for循环将尝试获取不存在的索引10。

代码语言:javascript
复制
static Scanner reader = new Scanner(System.in);

public static void main(String[] args) {    
    String pat = reader.nextLine();

    ArrayList<String> names = new ArrayList<String>();
    names.add("anna");
    names.add("bob");
    names.add("matthew");
    names.add("charles");
    names.add("effron");
    System.out.print(newArray(names, pat));
}

public static ArrayList<String> newArray(ArrayList<String> arr, String str) {
    ArrayList<String> alphabetized = new ArrayList<String>(arr.size());
    arr.add(str);
    java.util.Collections.sort(arr);
    for (int x=0; x<arr.size();x++){
        alphabetized.add(arr.get(x));
    }
    return alphabetized;
}

结果:

票数 2
EN

Stack Overflow用户

发布于 2015-04-20 19:11:03

问题是表达式alphabetized.get(x)是一个值,而不是变量,所以您不能为它赋值任何东西。这与数组访问表达式相反,数组访问表达式可以充当变量,例如alphabetizedArray[x] = array[x];

您需要 method,它接受索引和该索引的新值。

代码语言:javascript
复制
alphabetized.set(x, arr.get(x));
票数 5
EN

Stack Overflow用户

发布于 2015-04-20 19:13:51

若要更改ArrayList中的值,必须使用set方法。在您的例子中,替换alphabetized.get(x) = arr.get(x);的适当代码将是alphabetized.set(x, arr.get(x));,但是,我不明白为什么不能只返回arr而不是字母。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29756341

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档