Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/180.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
随机发生器的Java/Android OOP风格_Java_Android_Random_Coding Style - Fatal编程技术网

随机发生器的Java/Android OOP风格

随机发生器的Java/Android OOP风格,java,android,random,coding-style,Java,Android,Random,Coding Style,这是一个基本的编码风格问题,因为我试图在最佳实践上做到超级准确。我浏览并拼凑了一些Java代码,用于从列表中生成随机字符串,并将其添加到我的Android应用程序中的现有字符串中 在调查中我发现了两种方法,一种是使用一行代码通过从列表中选择一项来生成随机字符串,另一种是调用例程来完成基本相同的事情。我知道将代码分成更小的块是很好的,但是在这种情况下,一种方法通常会比另一种方法更可取吗?请注意,第一个选项在代码中被注释掉 protected void onCreate(Bundle savedIn

这是一个基本的编码风格问题,因为我试图在最佳实践上做到超级准确。我浏览并拼凑了一些Java代码,用于从列表中生成随机字符串,并将其添加到我的Android应用程序中的现有字符串中

在调查中我发现了两种方法,一种是使用一行代码通过从列表中选择一项来生成随机字符串,另一种是调用例程来完成基本相同的事情。我知道将代码分成更小的块是很好的,但是在这种情况下,一种方法通常会比另一种方法更可取吗?请注意,第一个选项在代码中被注释掉

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Get the message from the intent
    Intent intent = getIntent();
    String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

    //Add a random day to the string
    List<String> list = new ArrayList<>();
    list.add("Monday");
    list.add("Tuesday");
    list.add("Wednesday");
    list.add("Thursday");
    list.add("Friday");
    list.add("Saturday");
    list.add("Saturday");

    //Random rand = new Random();
    //String random = list.get(rand.nextInt(list.size()));

    String random = getRandom(list);

    message += " " + random;

    // Create the text view
    TextView textView = new TextView(this);
    textView.setTextSize(40);
    textView.setText(message);

    // Set the text view as the activity layout
    setContentView(textView);
}


static public <T> T getRandom(List<T> list){
    Random rand = new Random();
    if(list == null || list.isEmpty()){
        return null;
    }else{
        return list.get(rand.nextInt(list.size()));
    }
}
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//从意图中获得信息
Intent=getIntent();
字符串消息=intent.getStringExtra(MainActivity.EXTRA_消息);
//将随机日期添加到字符串中
列表=新的ArrayList();
列表。添加(“星期一”);
列表。添加(“星期二”);
列表。添加(“星期三”);
列表。添加(“星期四”);
列表。添加(“星期五”);
列表。添加(“星期六”);
列表。添加(“星期六”);
//Random rand=新的Random();
//String random=list.get(rand.nextInt(list.size());
字符串random=getRandom(列表);
消息+=“”+随机;
//创建文本视图
TextView TextView=新的TextView(此);
textView.setTextSize(40);
textView.setText(消息);
//将文本视图设置为活动布局
setContentView(文本视图);
}
静态公共T getRandom(列表){
Random rand=新的Random();
if(list==null | | list.isEmpty()){
返回null;
}否则{
return list.get(rand.nextInt(list.size());
}
}

在这种情况下,一个好主意是将代码移动到例程中。虽然函数调用可能会减慢应用程序的速度(无形中),但这将是代码重用的最佳方法。考虑到在另一个活动中需要相同的功能,可以简单地调用这个函数。