Add 10 names to array list and print them in reverse order using lamda expression

huangapple go评论84阅读模式
英文:

Add 10 names to array list and print them in reverse order using lamda expression

问题

这是我一直在尝试的代码:

public class Assignement2_java8 {

    public static void main(String[] args) {
        ArrayList<String> al = new ArrayList<String>();
        al.add("infosys");
        al.add("wipro");
        al.add("tcs");
        al.add("amazon");
        al.add("microsoft");
        al.add("google");
        al.add("acctenture");
        al.add("hcl");
        al.add("flipkart");
        al.add("apple");

        al.forEach(n -> System.out.println(new StringBuilder(n).reverse()));
    }
}

我知道我可以使用一个单词数组,然后将其存储在ArrayList中,但我想知道为什么我不能使用这个。

英文:

This is what I've been trying:

public class Assignement2_java8 {

	public static void main(String[] args) {
		ArrayList&lt;String&gt; al = new ArrayList&lt;String&gt;();
		al.add(&quot;infosys&quot;);
		al.add(&quot;wipro&quot;);
		al.add(&quot;tcs&quot;);
		al.add(&quot;amazon&quot;);
		al.add(&quot;microsoft&quot;);
		al.add(&quot;google&quot;);
		al.add(&quot;acctenture&quot;);
		al.add(&quot;hcl&quot;);
		al.add(&quot;flipkart&quot;);
		al.add(&quot;apple&quot;);

		al.forEach(n -&gt; System.out.println(n.reverse()));
	}
}

I know I can use an array of words then store it in ArrayList but I want to know why I can't use this.

答案1

得分: 0

// 添加10个名字到数组列表,并使用 lambda 表达式以逆序打印它们。

// ArrayList 没有逆序方法。创建一个接受 List 作为参数的自定义 Consumer。
Consumer<List<String>> reversePrint = lst -> {
    for (int i = lst.size()-1; i >= 0; i--) {
        System.out.println(lst.get(i));
    }
};

reversePrint.accept(al);  // 这里的 al 是你的 ArrayList

// 输出
// apple
// flipkart
// hcl
// acctenture
// google
// microsoft
// amazon
// tcs
// wipro
// infosys

// 如果你想要逐个打印每个字符串的逆序(与你的标题不同),可以像这样做。
al.forEach(n -> System.out.println(new StringBuilder(n).reverse()));

// 输出
// sysofni
// orpiw
// sct
// nozama
// tfosorcim
// elgoog
// erutnetcca
// lch
// trakpilf
// elppa
英文:

> Add 10 names to array list and print them in reverse order using lamda expression.

ArrayList does not have a reverse method. Create your own Consumer that takes a List as an argument.

Consumer&lt;List&lt;String&gt;&gt; reversePrint = lst-&gt; {
	for (int i = lst.size()-1; i &gt;= 0; i--) {
		 System.out.println(lst.get(i));
    }
};
		    
reversePrint.accept(al);

Prints

apple
flipkart
hcl
acctenture
google
microsoft
amazon
tcs
wipro
infosys

If you want to print each string in reverse (which is different than what your title says), then you can do it like this.

al.forEach(n -&gt; System.out.println(new StringBuilder(n).reverse()));

Prints

sysofni
orpiw
sct
nozama
tfosorcim
elgoog
erutnetcca
lch
trakpilf
elppa

</details>



huangapple
  • 本文由 发表于 2020年7月24日 06:17:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/63063930.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定