Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/398.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 对名称进行排序并使用LinkedList将其存储到数组单元格_Java_Arrays_Singly Linked List - Fatal编程技术网

Java 对名称进行排序并使用LinkedList将其存储到数组单元格

Java 对名称进行排序并使用LinkedList将其存储到数组单元格,java,arrays,singly-linked-list,Java,Arrays,Singly Linked List,因此,我们的想法是获取一个输入,它是一个字符串(一个特定的名称),然后将其存储在一个数组中,每个单元格的大小为26。排序是这样的:以“A”开头的名称进入单元格0,以“B”开头的名称进入单元格1,依此类推。现在,一个单元格包含一个LinkedList,其中名称按字母顺序再次排序 到目前为止,我采用的方法是使用switch case private void addDataAList(AuthorList[] aL, String iN) { char nD = Character.toUp

因此,我们的想法是获取一个输入,它是一个
字符串
(一个特定的名称),然后将其存储在一个
数组
中,每个单元格的大小为26。排序是这样的:以“A”开头的名称进入单元格0,以“B”开头的名称进入单元格1,依此类推。现在,一个单元格包含一个
LinkedList
,其中名称按字母顺序再次排序

到目前为止,我采用的方法是使用switch case

private void addDataAList(AuthorList[] aL, String iN) {
    char nD = Character.toUpperCase(iN.charAt(0));
        switch(nD){
            case 'A':
                AuthorList[0] = iN;
            break;

            case 'B':
                AuthorList[1] = iN;
            break;
            //and so on
        }
}//addData

有更有效的方法吗?

假设AuthorList类可能如下所示:

private class AuthorList{
    private LinkedList<String> nameList;

    public AuthorList() {
    }

    public AuthorList(LinkedList<String> nameList) {
        this.nameList = nameList;
    }

    public LinkedList<String> getNameList() {
        return nameList;
    }

    public void setNameList(LinkedList<String> nameList) {
        this.nameList = nameList;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("AuthorList{");
        sb.append("nameList=").append(nameList);
        sb.append('}');
        return sb.toString();
    }
}
private static void addDataAList(AuthorList[] aL, String iN) {
    int index = Character.toUpperCase(iN.trim().charAt(0)) - 'A';
    try {
        AuthorList tmpAuthorList = aL[index];
        if(tmpAuthorList == null) aL[index] = tmpAuthorList = new AuthorList(new LinkedList<>());
        if(tmpAuthorList.getNameList() == null) tmpAuthorList.setNameList(new LinkedList<>());
        tmpAuthorList.getNameList().add(iN);
    } catch (ArrayIndexOutOfBoundsException aioobe){
        throw new IllegalArgumentException("Name should start with character A - Z");
    }
}

您是否尝试过在中使用
AuthorList[nD-'A']=iN?@OldCurmudgeon不,谢谢。我甚至不知道你们可以这样做。但你们需要以某种方式防止BoundException的出现。例如,捕获它并抛出新的IllegalArgumentException,其中包含关于大写字母要求的适当消息。在.trim()中也可能有用。@MariuszSzurgot会的,只要我对代码进行最后润色。@OldCurmudgeon这一个以“so”开头!!列表不是一般的iirc,还是应该是ArrayList?无论哪种方式,我都会尝试用您的代码建模,看看它是否有效,然后才能接受这个答案。我不确定AuthorList是什么,以及如何向其中添加数据,但我确定将字符串分配给AuthorList[x]并带有“=”将失败,因此我已对其进行了更改,以使其在测试中正常工作。当然,您应该使用AuthorList类使此解决方案适合您的需要。
public static void main (String[] args){
    AuthorList[] aL = new AuthorList[26];
    addDataAList(aL, " dudeman");
    for (AuthorList list : aL) System.out.println(list);
}