I have this code:

public static void main(String[] args) throws Exception
{
    //Read strings and init ArrayList list here
    ArrayList<String> list = new ArrayList<String>();
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    for (int i = 0; i < 10; ++i)
        list.add(reader.readLine());
    ArrayList<String> result = doubleValues(list);
    for (int i = 0; i < 20; ++i)
        System.out.println(result.get(i));
}

public static ArrayList<String> doubleValues(ArrayList<String> list)
{
    for (int i = 0; i < 10; i += 2) {
        list.add(i+1, list.get(i));
    }
    return list;
}

Result is fine, but it don't passes test. What is wrong with it?

asked 14 Apr, 13:20

noelo's gravatar image

noelo
102
accept rate: 0%

edited 14 Apr, 13:21


This code passed the test when I change

i < 10

in doubleValues method to

i < list.size()

.

link

answered 14 Apr, 13:22

noelo's gravatar image

noelo
102
accept rate: 0%

Passed test.

public class Solution
{
    public static void main(String[] args) throws Exception
    {
        BufferedReader scan = new BufferedReader(new InputStreamReader(System.in));
        ArrayList<String> list = new ArrayList<String>();
        for(int i = 0; i < 10; i++){
            list.add(scan.readLine());
        }

        ArrayList<String> result = doubleValues(list);

        for(String element : result){
            System.out.println(element);
        }
    }

    public static ArrayList<String> doubleValues(ArrayList<String> list)
    {
        for(int i=0; i < list.size(); i++){
            list.add(i, list.get(i));
            i++;
        }
        return list;
    }
}
link

answered 25 May, 11:22

Igor's gravatar image

Igor
112
accept rate: 0%

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×6

Asked: 14 Apr, 13:20

Seen: 279 times

Last updated: 25 May, 11:22