344 - ahmad alhayek

344

 

Write a method that returns an array list of Character from a string using the following header: public static ArrayList<Character> toCharacterArray (String s) For example toCharacterArray(abc) returns an array list that contains characters a, b and c Write a test method that prompts the user to enter a String. Call the method toCharacterArray0 on the input string. Print the elements of the Character array list separated by exactly one space Here is a sample run: Enter the input string: Stony Brook

Show transcribed image text

Expert Answerinformation icon

  • Ronaldo's Avatar
    //StringToArrayList.java
    import java.util.ArrayList;
    import java.util.Scanner;
    
    public class StringToArrayList {
        public static ArrayList<Character> toCharacterArray(String s){
            ArrayList<Character> list = new ArrayList<Character>();
            for(char ch: s.toCharArray()){
                list.add(ch);
            }
            return list;
        }
    
        public static void main(String args[]) {
            String s;
            Scanner scanner = new Scanner(System.in);
            System.out.print("Enter input string: ");
            s = scanner.nextLine();
            ArrayList<Character> list = toCharacterArray(s);
            System.out.print("Elements of the character array list: ");
            for(int i = 0;i<list.size();i++){
                System.out.print(list.get(i) + " ");
            }
        }
    }
    

    Enter input string: Stony Brook Elements of the character array list:Stony Bro ok Process finished with exit code 0

    \color{red}Please\; upvote\;the \;solution \;if \;it \;helped.\;Thanks!

    Comment 

ليست هناك تعليقات:

إرسال تعليق

ahmad alhayek تصميم ahmad alhayek جميع الحقوق محفوظة 2016

صور المظاهر بواسطة sndr. يتم التشغيل بواسطة Blogger.