Remove specific index from array in java

前端 未结 6 1321
情歌与酒
情歌与酒 2020-12-11 23:35

Can I remove a specific element from array by mentioning index value? For example can I remove the character d by giving index value 1?

<         


        
6条回答
  •  再見小時候
    2020-12-12 00:16

    If you don't want to use ArrayList, arraycopy is an alternative:

        System.arraycopy(words, 0, result, 0, i);
        System.arraycopy(words, i+1, result, i, result.length-i);
    

    where i is your index to delete.

    Hope I can help.

    EDIT: Of course you should initially define the correct array lengths:

    char[] result = new char[words.length-1];
    

提交回复
热议问题