用更少的代码在Java中初始化变量的更好方法是什么?
在下面的示例中,压缩代码的更好的选择(最简单)是什么 我想有一种方法可以循环或排列这些。当我试图将它们放入数组时,语法错误就消失了。也许arrayList或treeMap是更好的方法?我对那些结构有点生疏了。我是不是想深入调查这件事用更少的代码在Java中初始化变量的更好方法是什么?,java,loops,user-interface,variables,initialization,Java,Loops,User Interface,Variables,Initialization,在下面的示例中,压缩代码的更好的选择(最简单)是什么 我想有一种方法可以循环或排列这些。当我试图将它们放入数组时,语法错误就消失了。也许arrayList或treeMap是更好的方法?我对那些结构有点生疏了。我是不是想深入调查这件事 private JMenuItem one = new JMenuItem("One"); private JMenuItem two = new JMenuItem("Two");
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");
你可以做的一件事就是将每个项添加到一个arraylist中,这样你就不用有67个变量来跟踪了,你只需要担心一个arraylist。你不能在方法之外有一个
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
}
private JMenuItem[]menuItems=new JMenuItem[67];
{
//此代码位于初始值设定项块中,将
//无论调用哪个构造函数,都要运行。
对于(int i=0;i
有关使用Java8实现的
numberToWords
的信息,请参阅。我将在以后进行流式处理并收集它。这样可以节省很多线路
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());
}
private static final int START=1;
专用静态最终int END=67;
私人名单;
私有无效初始值列表(){
/*您将需要ConvertIntToSplledString(int)方法
*正如@Andy Thomas在其评论中提到的那样
*/
this.list=IntStream.rangeClosed(START,END).boxed()
.map(i->new JMenuItem(convertIntToSplledString(i))
.collect(Collectors.toList());
}
方法Integer.toString()
将为您提供数字,而不是单词,例如,“67”,而不是“67”.另请参见。是否有更好的方法来实现您的目标取决于您实际尝试的内容。您是否真的使用英文数字拼写创建菜单项?如果您将67个菜单项放在一个菜单中,则有比一个庞大而笨拙的菜单更好的UI习惯用法来实现该功能…什么是数组的语法错误?因为这是一个非常好的想法,就像每个人都在介绍的想法一样。我不确定numberToWords方法是否会简化任何东西。我以数字为例,希望找到一个简单的解决方案,但我的代码稍微复杂一些。下面是实际的用例:代码视图私有JMenu文件=新建JMenu(“文件”);专用JMenu编辑=新建JMenu(“编辑”);专用JMenu格式=新建JMenu(“格式”);专用JMenu视图=新建JMenu(“视图”);专用JMenu帮助=新建JMenu(“帮助”);private JMenuItem save=新建JMenuItem(“保存”);private JMenuItem open=新建JMenuItem(“打开”);private JMenuItem exit=新建JMenuItem(“退出”);您可以将JFrames
的名称分配到一个单独的属性文件中,并给它们一个键
您可以这样迭代(即通过一个可计数的字母数字键)。但是,如果这是一个更简单的项目,您还可以创建一个EnumClass
来存储这些特定的名称
,以便在数据
和实际逻辑
方面实现关注点的分离