用更少的代码在 Java 中初始化变量的更好方法是什么?
What is a better way to initialize varibles in Java with less code?
在下面的示例中,什么是更好的(最简单的)压缩代码的替代方法?
我在想可能有一种方法可以循环或排列这些。当我试图将它们放在一个数组中时,出现了语法错误。也许 arrayList 或 treeMap 会是更好的方法?我对这些结构有点生疏。我想深入了解一下吗?
private JMenuItem one = new JMenuItem("One");
private JMenuItem two = new JMenuItem("Two");
//... = ...;
//... = ...;
private JMenuItem sixtySeven = new JMenuItem("Sixty Seven");
for(conditions or array looping)
{
private JMenuItem i = new JMenuItem(i.toString().proper());//I have to research if there is even a "proper" method.
}
//Revised example below:
private JMenu edit = new JMenu("Edit");
private JMenu format = new JMenu("Format");
private JMenu view = new JMenu("View");
private JMenu help = new JMenu("Help");
private JMenuItem save = new JMenuItem("Save");
private JMenuItem open = new JMenuItem("Open");
private JMenuItem exit = new JMenuItem("Exit");
你可以做的一件事就是将每个项目添加到一个数组列表中,这样你就不用担心 67 个变量来跟踪一个数组列表。
你不能在方法之外有一个 for
循环,如果你把它移到方法内部,那么你会声明局部变量,所以 private
是不允许的.
而是使用 数组:
private JMenuItem[] menuItems = new JMenuItem[67];
{
// This code is in an initializer block and will
// run regardless of which constructor is called.
for (int i = 0; i < menuItems.length; i++) {
this.menuItems[i] = new JMenuItem(numberToWords(i + 1));
}
}
private static String numberToWords(int number) {
// Code here
}
有关 numberToWords
的实施,请参阅 How to convert number to words in java。
使用 Java 8 我会去流媒体然后收集它。那应该可以节省很多行。
private static final int START = 1;
private static final int END = 67;
private List<JMenuItem> list;
private void initializeList(){
/*You will need the convertIntToSpelledString(int) method
*as @Andy Thomas mentioned above in his comment
*/
this.list = IntStream.rangeClosed(START, END).boxed()
.map(i -> new JMenuItem(convertIntToSpelledString(i))
.collect(Collectors.toList());
}
在下面的示例中,什么是更好的(最简单的)压缩代码的替代方法?
我在想可能有一种方法可以循环或排列这些。当我试图将它们放在一个数组中时,出现了语法错误。也许 arrayList 或 treeMap 会是更好的方法?我对这些结构有点生疏。我想深入了解一下吗?
private JMenuItem one = new JMenuItem("One");
private JMenuItem two = new JMenuItem("Two");
//... = ...;
//... = ...;
private JMenuItem sixtySeven = new JMenuItem("Sixty Seven");
for(conditions or array looping)
{
private JMenuItem i = new JMenuItem(i.toString().proper());//I have to research if there is even a "proper" method.
}
//Revised example below:
private JMenu edit = new JMenu("Edit");
private JMenu format = new JMenu("Format");
private JMenu view = new JMenu("View");
private JMenu help = new JMenu("Help");
private JMenuItem save = new JMenuItem("Save");
private JMenuItem open = new JMenuItem("Open");
private JMenuItem exit = new JMenuItem("Exit");
你可以做的一件事就是将每个项目添加到一个数组列表中,这样你就不用担心 67 个变量来跟踪一个数组列表。
你不能在方法之外有一个 for
循环,如果你把它移到方法内部,那么你会声明局部变量,所以 private
是不允许的.
而是使用 数组:
private JMenuItem[] menuItems = new JMenuItem[67];
{
// This code is in an initializer block and will
// run regardless of which constructor is called.
for (int i = 0; i < menuItems.length; i++) {
this.menuItems[i] = new JMenuItem(numberToWords(i + 1));
}
}
private static String numberToWords(int number) {
// Code here
}
有关 numberToWords
的实施,请参阅 How to convert number to words in java。
使用 Java 8 我会去流媒体然后收集它。那应该可以节省很多行。
private static final int START = 1;
private static final int END = 67;
private List<JMenuItem> list;
private void initializeList(){
/*You will need the convertIntToSpelledString(int) method
*as @Andy Thomas mentioned above in his comment
*/
this.list = IntStream.rangeClosed(START, END).boxed()
.map(i -> new JMenuItem(convertIntToSpelledString(i))
.collect(Collectors.toList());
}