LinkedList实现字符串反转输出

题目描述:

从控制台中读取一段字符串,利用LinkedList中的方法将该字符串反转输出在控制台中.
当输入为123456时
输出为:654321

import java.util.LinkedList;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		
		//write your code here

		Scanner sc = new Scanner(System.in);
		String text = sc.next();
		//转化为集合字符集合
		char[] ch = text.toCharArray();
		LinkedList<Character> linkedList = new LinkedList<Character>();
		for(Character c : ch){
			linkedList.add(c);
		}
		for(int i = linkedList.size()-1; i >= 0; i--){
			System.out.print(linkedList.get(i));
		}

	}
}

posted on 2022-01-26 15:13  爱Java的小白  阅读(312)  评论(0)    收藏  举报

导航