简体   繁体   English

无法按正确顺序将元素添加到ArrayList

[英]Having trouble adding elements to ArrayList in correct order

So my program is supposed to iterate over a list of numbers (0-113) and then print the numbers line by line. 因此,我的程序应该遍历数字列表(0-113),然后逐行打印数字。 If the number is odd, that should be added next to the number, if the number is divisible by 5, that should be added next to the number and so on. 如果数字是奇数,则应在数字旁边添加,如果数字可被5整除,则应在数字旁边添加,依此类推。 So I have my 4 boolean methods done and they look like this: 所以我完成了4个布尔方法,它们看起来像这样:

public static boolean isDivisibleBy5(int n){        
    if (n%5 ==0){           
        return true;
    }       
    return false ;          
}

Next I have a method that I am required to use as per assignment requirements and it is as follows: 接下来,我有一种根据分配要求使用的方法,如下所示:

public static ArrayList<String>iterate()

So the definition with this method is where I am having problems. 因此,使用这种方法的定义就是我遇到问题的地方。 Right now what I have is this: 现在我所拥有的是:

public static ArrayList<String>iterate(){
    ArrayList<String> list = new ArrayList<String>();
    for(int i=0;i<114;i++){

    if (isOdd(i)){
    list.add(i+" Is odd");
    }
    if(isDivisibleBy5(i)){
        list.add(i+" hi five");
    }
    if(isDivisbleBy7(i)){
        list.add(i+" wow");
    }
    if (isPrime(i)){
        list.add(i+" prime");
    }
    }
    for(String elem:list)
    System.out.println(elem);
    return list;

}

But unfortunately my output looks like this: 但不幸的是,我的输出看起来像这样:

0 hi five
1 Is odd
1 prime
3 Is odd
3 wow
3 prime
5 Is odd
5 hi five
5 prime
7 Is odd
7 prime
9 Is odd
10 hi five
10 wow
11 Is odd

I need it to look like this: 我需要它看起来像这样:

0, hi five
1, Is Odd, prime
2
3, Is odd, wow, prime
4
5, Is Odd, hi five, prime

etc. 等等

So my question is basically how do I get all the conditions (when true) to add themselves to the same line with the corresponding number and also have the numbers that don't meet the conditions print on their lines as well, like 2 and 4 and 6. I've been stuck on this question for awhile and I feel like there is a crucial piece of java that I am not thinking of that is required here. 所以我的问题基本上是我如何获取所有条件(当为true时)以将自己添加到具有相应编号的同一行中,并且还要将不满足条件的数字也打印在其行上,例如2和4和6.我已经在这个问题上停留了一段时间了,我觉得这里有一个关键的Java我不认为这是必需的。 String Builder maybe? 字符串生成器也许? I'm not sure. 我不确定。 Any help is appreciated. 任何帮助表示赞赏。 Even if you can just point me to a concept to look up and learn more of. 即使您可以为我指出一个可以查找和学习更多内容的概念。

Thanks 谢谢

EDIT: I am seeing a lot of responses about building the string first and then adding to it. 编辑:我看到很多关于首先构建字符串然后添加到它的响应。 I believe this was the concept that I was not thinking of that has held me back so I will read up and practice implementing that. 我相信这是我没有想到的概念,使我受挫,因此我将继续阅读并练习实施该概念。 Thanks again for the quick responses. 再次感谢您的快速回复。 When I get it working I will come back here and up-vote anyone who helped. 当我开始工作时,我将回到这里,对任何帮助过的人进行投票。

You need to compose the string first and then add it only once: 您需要先组成字符串,然后才添加一次:

public static ArrayList<String>iterate(){
    ArrayList<String> list = new ArrayList<String>();
    for(int i=0;i<114;i++){
        String toAdd = String.valueOf(i);    

        if (isOdd(i)){
            toAdd += ", Is odd";
        }
        if(isDivisibleBy5(i)){
            toAdd += ", hi five";
        }
        if(isDivisbleBy7(i)){
            toAdd += ", wow";
        }
        if (isPrime(i)){
            toAdd += ", prime";
        }

        list.add(toAdd);
    }

    for(String elem:list)
        System.out.println(elem);

    return list;    
}

So from what I see you're using list.add() and its adding a row every time you call it. 因此,从我的角度来看,您正在使用list.add(),并且每次调用它时都会添加一行。 Instead of using list.add() in every if statement, why don't you just add everything to a sting and then use list.add(string) to add it to your list? 而不是在每个if语句中都不使用list.add(),为什么不直接将所有内容添加到字符串中,然后使用list.add(string)将其添加到列表中呢?

Something like this: 像这样:

public static ArrayList<String>iterate(){
    String line =""; //creates line
    ArrayList<String> list = new ArrayList<String>();
    for(int i=0;i<114;i++){
    line += i;
    if (isOdd(i)){
    line +=", Is odd";
    }
    if(isDivisibleBy5(i)){
     line += ", high five"
    }
    ...
    list.add(line);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM