我尝试在没有任何for循环的情况下打印输入数字的反向。但是我在打印Arraylist时遇到了一些问题。我怎么能打印Arraylist [3,5,4,1]为3541 - 没有括号,逗号和空格?Java:ArrayList to String?
如果不行,我该如何将我的ArrayList元素添加到字符串列表然后打印?
public static void main(String[] args) {
int yil, bolum = 0, kalan;
Scanner klavye = new Scanner(System.in);
ArrayList liste = new ArrayList();
//String listeStr = new String();
System.out.println("Yıl Girin: "); // enter the 1453
yil = klavye.nextInt();
do{ // process makes 1453 separate then write in the arraylist like that [3, 5, 4,1]
kalan = yil % 10;
liste.add(kalan);
bolum = yil/10;
yil = bolum;
}while(bolum != 0);
System.out.println("Sayının Tersi: " + ....); //reverse of the 1453
klavye.close();
}
的http://stackoverflow.com/questions/196628/printing-out-items-in-any-collection-in-reverse-order – r0ast3d
你为什么需要重复避免for循环? – jlordo
你可以读取字符串而不是数字(或者你可以从int转换为String)并使用StringBuilder reverse()。 – nhahtdh