More Sam-I-Am

  • 5
  • Locked
From now on, green eggs and ham will be overshadowed by lists. Or would the opposite be better? Here's our task: add some of Dr. Suess's famous words to a list: "Sam", "I", "Am". After each word, we'll insert "Ham" into the list. And display the result on the screen. Each list element on a new line.
You can't complete this task, because you're not signed in.
Comments (12)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Level 7 , London, United Kingdom
2 September 2021, 12:05
Is there a better way to do this than manually placing each "Ham" into the list? Could it be written into a for loop for example?
Pranai Rao Head of Software at FTC Team 8888
26 November 2022, 15:12
Yes there is:
public class Solution {
    public static void main(String[] args) throws Exception {
        //write your code here
        ArrayList<String> list = new ArrayList<String>();

        for (int x = 0; x < list.size(); x++) {
            if (list.get(x).equals("Ham")) {continue;}
            if (x == list.size()) {break;}
            else {list.add(x+1, "Ham");}

        int size = list.size();
        for (int y = 0; y < size; y++) {
Level 15 , Old Town, United States
21 April 2021, 00:33
although the following is acceptable way to do it, it doesn't pass. FYI, ArrayList<String> list = new ArrayList<>(Arrays.asList("Sam", "I", "Am"));
Nickolas Johnson
Level 8 , St. Louis, United States of America
26 March 2020, 14:34
Seeing as the instructions are unclear, you have to enter each word individually and only use one ArrayList for the "Declare a string list variable and immediately initialize it" to work. This exercise is more about showing that you can dynamically insert words between other words in an ArrayList. Don't do what I did and spend 45 minutes overcomplicating it for it to run correctly but still not pass verification.
Kent Hervey Software Engineer/Consult at Zeal IT ConsultantsExpert
15 November 2019, 03:14
Verifier does not like this: ArrayList<String> list = new ArrayList<>(Arrays.asList("Sam","I","Am"));
dabay wang Backend Developer at Amazon
24 March 2020, 16:37
It wants you add one by one... unfortunately.
Level 34 , Phnom Penh, Cambogia
29 May 2019, 23:30
Someone can display the correct result: all below is wrong... 0 Sam 1 Ham 2 I 1 Ham 4 am 1 Ham _____________________ 1 Ham 4 Am 1 Ham 2 I 1 Ham 0 Sam
Level 41
30 May 2019, 06:32
am != Am (first result)
Level 11 , Spokane, United States
2 May 2019, 02:41
This one could do with the example output, as I first created a code which output: SamHam IHam AmHam which was actually more challenging to do than the intended output.
Arko Sarkar
Level 8 , Mumbai, India
31 August 2018, 15:22
Should the output be: Sam Ham I Ham Am Ham OR SamHam IHam AmHam ?
Level 41
3 September 2018, 05:45
Level 7 , Chernihiv, Ukraine
23 March 2019, 16:44