Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.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 在歌曲标题数组中删除和插入元素_Java_Arrays_Arraylist - Fatal编程技术网

Java 在歌曲标题数组中删除和插入元素

Java 在歌曲标题数组中删除和插入元素,java,arrays,arraylist,Java,Arrays,Arraylist,我正在学习如何自学在数组中删除和插入元素。这是我正在使用的数组,它只是一组歌曲标题: public class Algorithms { public static void main () {Song[] topTenSongs = {new Song("The Twist"), new Song ("Smooth"), new Song ("Mack the Knife"), new Song ("How Do I Live"),

我正在学习如何自学在数组中删除和插入元素。这是我正在使用的数组,它只是一组歌曲标题:

public class Algorithms
{

public static void main ()
{Song[] topTenSongs = {new Song("The Twist"), 
        new Song ("Smooth"),
        new Song ("Mack the Knife"),
        new Song ("How Do I Live"),
        new Song ("Party Rock Anthem"),
        new Song ("I Gotta Feeling"),
        new Song ("Macarena (Bayside Boys Mix)"),
        new Song ("Physical"),
        new Song ("You Light Up My Life"),
        new Song ("Hey Jude")
    };

    String[] tenSongNames = {"The Twist",
        "Smooth",
        "Mack the Knife",
        "How Do I Live",
        "Party Rock Anthem",
        "I Gotta Feeling",
        "Macarena (Bayside Boys Mix)",
        "Physical",
        "You Light Up My Life",
        "Hey Jude"};


    for (int i = 0; i < topTenSongs.length; i++)
    {topTenSongs [i].setTitle(tenSongNames[i]);
        System.out.println(topTenSongs [i].getTitle());


}
公共类算法
{
公共静态void main()
{Song[]topTenSongs={新歌(“扭曲”),
新歌(“平滑”),
新歌(“刀麦克”),
新歌(“我如何生活”),
新歌(“派对摇滚歌”),
新歌(“我要感受”),
新歌(“Macarena(Bayside男孩混音)”,
新歌(“物理”),
新歌(“你照亮了我的生活”),
新歌(“嘿,裘德”)
};
String[]tenSongNames={“扭曲”,
“顺利”,
“刀麦克”,
“我如何生活”,
“派对摇滚歌”,
“我有感觉”,
“Macarena(海湾男孩组合)”,
“物理”,
“你照亮了我的生活”,
“嘿,裘德”};
for(int i=0;i
我的目标是创建一个for循环来迭代歌曲数组的长度。我正在删除歌曲标题“我要感觉”从数组的中间。我想将数组中的下一首歌曲复制到当前位置,然后继续迭代,将下一首歌曲替换到当前位置,直到到达数组的末尾。然后,我想编写一个新的for循环,以显示新的较短歌曲列表的内容(数组的最后一个索引中的最后一个点9现在应该是“null”)

这就是我到目前为止所做的。我对整个迭代过程感到困惑,在这个过程中,我可以将歌曲列表上移到更早的索引中

int chosenIndex = 0;
for (int i = 0; i <topTenSongs.length; i ++) {
if (topTenSongs[i].getTitle().equals("I Gotta Feeling")==true){
chosenIndex = i;}} 

for (int i = chosenIndex; i < topTenSongs.length -1; i++) {
topTenSongs[i] = topTenSongs[i+1];
if(topTenSongs[i+1].getTitle().equals("Macarena (Bayside Boys Remix")==true){
    chosenIndex = i+1;
    String iteration = topTenSongs[i+1].getTitle();}
}

for (int i = 0; i< topTenSongs.length; i++) {
System.out.println(topTenSongs);
}
int chosenIndex=0;

for(int i=0;i我假设你想专门使用数组,这样你就可以练习for循环什么的。否则,@CarlosHeuberger指出,使用
ArrayList
会容易得多

让我们看一下你目前的情况

int chosenIndex = 0;
for (int i = 0; i <topTenSongs.length; i ++) {
if (topTenSongs[i].getTitle().equals("I Gotta Feeling")==true){
chosenIndex = i;}} 
老实说,我不知道你想在这里完成什么。看起来你想将所有剩余的歌曲向左移动以填补空白,这是你在开始时所做的,但我不知道循环的第二部分的目的是什么。如果它与特定歌曲匹配,你将跳过索引,但是如果你使用不同的歌曲呢数组,其中没有与“Macarena”匹配的歌曲?尝试避免像这样脆弱的编码,并制定一个可以处理任何数据的解决方案

您的想法是正确的,但您需要仔细考虑一下。让我们仔细考虑一下如何执行此操作。当我们选择要删除的索引时,我们希望:

  • chosenIndex
    开始,迭代列表
  • 用下一首歌的索引替换每个索引处的元素
  • 除非我们到达列表的末尾,在这种情况下,我们需要将该元素设置为null
  • 现在,让我们把它们放在一起:

    for (int i = chosenIndex; i < topTenSongs.length; i++) {
        if(i == topTenSongs.length-1) {
            topTenSongs[i] = null; //set last element to null
        } else {
            topTenSongs[i] = topTenSongs[i+1];
        }
    }
    

    作为一项附加任务,请尝试使用此代码并创建一种方法,您可以调用任何歌曲数组以从中删除具有特定标题的歌曲。

    @CarlosHeuberger您好!我可以通过简单地向上移动每首歌曲以替换取出的歌曲来保持数组的大小不变。取出的歌曲现在将被表示出来例如,在数组的最后一个点上有一个“null”插槽。
    for (int i = chosenIndex; i < topTenSongs.length -1; i++) {
        topTenSongs[i] = topTenSongs[i+1];
        if(topTenSongs[i+1].getTitle().equals("Macarena (Bayside Boys Remix")){
            chosenIndex = i+1;
            String iteration = topTenSongs[i+1].getTitle();
        }
    }
    
    for (int i = chosenIndex; i < topTenSongs.length; i++) {
        if(i == topTenSongs.length-1) {
            topTenSongs[i] = null; //set last element to null
        } else {
            topTenSongs[i] = topTenSongs[i+1];
        }
    }
    
    for (int i = 0; i < topTenSongs.length; i++) {
        if(topTenSongs[i] != null) {
            System.out.println(topTenSongs[i].getTitle());
        } else {
            System.out.println("null");
        }
    }