一道朋友java面试的优化题 ,不得解

优化以下代码


 import java.util.ArrayList;


    public class Test {

    

        public static void main(String args[]) {

            ArrayList<String> list = new ArrayList<>();

    

            String prefix = "pre";

    

            for (int i = 0; i < 400; i++) {

                String s = prefix + i;

                list.add(s);

            }

            System.out.println(list);

        }

    }

不太清楚出题者的意图? String的问题?貌似是,但是不知道怎么改


侃侃尔雅
浏览 433回答 6
6回答

30秒到达战场

优化后代码如下:&nbsp; &nbsp; import java.util.List;&nbsp; &nbsp; import java.util.ArrayList;&nbsp; &nbsp; public class Test {&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; public static void main(String args[]) {&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List<String> list = new ArrayList<String>();&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; StringBuilder prefix = new StringBuilder("pre");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int max = 400;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < max; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int beforeAppLen = prefix.length();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; prefix.append(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list.add(prefix.toString());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; prefix.delete(beforeAppLen, prefix.length());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(list);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }1、ArrayList 改为 List,体现继承的地方。2、优化 String,改为使用 StringBuilder,性能优化。3、提取局部变量400,使用有意义的变量命名,可读性问题。

四季花海

import java.util.ArrayList;public class Test {&nbsp; &nbsp; public static void main(String args[]) {&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < 400; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(prefix + i);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}

叮当猫咪

优化点应该是减少for循环中产生的对象for (int i = 0; i < 400; i++) {&nbsp; &nbsp; list.add(prefix + i);}

繁华开满天机

ArrayList<String> list = new ArrayList<>(400);&nbsp; &nbsp; &nbsp; &nbsp; char[] chars = new char[]{'p','r','e','\0','\0','\0'};&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < 10; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; chars[3] = (char) (i + 48);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list.add(new String(chars, 0, 4));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 10; i < 100; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; chars[4] = (char) ((i % 10) + 48);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; chars[3] = (char) ((i / 10) + 48);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list.add(new String(chars, 0, 5));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 100; i < 400; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; chars[5] = (char) ((i % 10) + 48);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; chars[4] = (char) (((i / 10) % 10) + 48);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; chars[3] = (char) ((i / 100) + 48);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list.add(new String(chars, 0, 6));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(list);仅考虑性能的话 不管怎么变着花的用StringBuilder都不如亲自操作数组来的高效,不过需要手动转换数字对于这道题来说 循环四百次 用一个长度为6的char数组即可满足要求对于ArrayList动态扩容也是需要时间的,所以初始化时把长度写申请足了也会起一点作用。。

收到一只叮咚

理论上以下通过以下两处更改可以优化,但我试了下指定初始化list容量,性能反倒降低。很是纳闷,还是请高手解答吧。&nbsp;import java.util.ArrayList;&nbsp; &nbsp; public class Test {&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; public static void main(String args[]) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List<String> list = new ArrayList<String>(400);&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final String prefix = "pre";&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < 400; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String s = prefix + i;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list.add(s);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(list);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java