简体   繁体   English

如何在外部循环中从[0到9]访问字符串列表?

[英]How can I access the Strings list from [0 to 9] outside for loop?

I am trying to write a code that allows me to access String msgbody and place it in an email. 我试图编写一个代码,使我可以访问String msgbody并将其放置在电子邮件中。 However, using the following code only the final String shows up outside the for loop. 但是,使用以下代码,最终字符串仅显示在for循环之外。 I searched many times for an answer, and am a bit new to Java. 我搜索了很多次答案,并且对Java有点陌生。

    List<String> urls = new ArrayList<String>();
    String msgbody = "";
       for (int i = 0; i < urls.size(); i++) {
            //urls.get(i); unecessary
            // String msgbody0; - Used to attempt msgbody0 = url.get(i); then msgbody += msgbody0; but only prints first url
            int sum = i + 1;
            for (j = 1; j < 2 ; j++)
            {
                msgbody = urls.get(i);

                //urls.add(msgbody); not needed
                System.out.print("("+ sum +")" +"");
                System.out.println(msgbody); 
            }
            if(i==9){
                break;
            }
        } // Inside the for loop everything prints properly.

       System.out.println(msgbody); // How can i replicate it here?(Outside for loop)

Edit: THIS worked: (Initially -The following only shows the first link repeatedly:) I changed from Java to Java EE and it worked? 编辑:这有效:(最初-以下仅重复显示了第一个链接:)我从Java更改为Java EE,它有效吗? What was i missing? 我想念什么?

    msgbody += urls.get(i);

I can make it work without the for loop, but its sloppy. 我可以使它在没有for循环的情况下工作,但它马虎。

You could change msgbody = urls.get(i); 您可以更改msgbody = urls.get(i); to msgbody += urls.get(i); msgbody += urls.get(i); (otherwise you're completely replacing the msgbody with each iteration). (否则,您将在每次迭代中完全替换msgbody )。 Also, I would prefer a StringBuilder , the diamond operator <> and a for-each loop . 另外,我更喜欢StringBuilder菱形运算符<>for-each循环 Like, 喜欢,

List<String> urls = new ArrayList<>();
StringBuilder sb = new StringBuilder();
for (String url : urls) {
    sb.append(urls.get(url));
}
System.out.println(sb);

only the minor change is that you are replacing msgbody rather than to concatenate it. 仅有的微小变化是您要替换msgbody而不是将其串联。

List<String> urls = new ArrayList<String>();
    String msgbody = "";
       for (int i = 0; i < urls.size(); i++) {
            //urls.get(i); unecessary
            // String msgbody0; - Used to attempt msgbody0 = url.get(i); then msgbody += msgbody0; but only prints first url
            int sum = i + 1;
            for (j = 1; j < 2 ; j++)
            {
                msgbody += urls.get(i);

                urls.add(msgbody);
                System.out.print("("+ sum +")" +"");
                System.out.println(msgbody); 
            }
            if(i==9){
                break;
            }
        } // Inside the for loop everything prints properly.

       System.out.println(msgbody); 

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

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