小编mre*_*mre的帖子

返回NULL的替代方法

   /**
     * Returns the foo with the matching id in this list
     * 
     * @param id the id of the foo to return
     * @return the foo with the matching id in this list
     */
    public Foo getFoo(int id)
    {
        for (Foo foo : list)
        {
            if (foo.getID() == id)
            {
                return foo;
            }
        }

        return null;
    }
Run Code Online (Sandbox Code Playgroud)

而不是返回的null时候foo没有发现,我应该throwexception?这是否重要,是否有关于这个问题的"最佳实践"成语?顺便说一下,我知道我的例子有点做作,但我希望你明白这个想法......

谢谢.

编辑

更改了Foo基于id的代码,以更好地说明真实场景.

java null design-patterns return-value

25
推荐指数
3
解决办法
4081
查看次数

使用枚举值和注释进行Java字符串验证

我想使用注释针对一组值验证字符串.我想要的基本上就是这个

@ValidateString(enumClass=com.co.enum)
String dataType;

int maxValue;
int minValue;
int precision;
Run Code Online (Sandbox Code Playgroud)

要么

@ValidateString(values={"String","Boolean", "Integer"})
String dataType;

int maxValue;
int minValue;
int precision;
Run Code Online (Sandbox Code Playgroud)

我还想根据dataType中设置的值对其他变量进行一些验证,

if(dataType ="String")maxValue,minValue,precision all应为null或0.

我无法想到通过自定义注释实现这一目标的方法..

有人请帮帮我

java enums annotations

25
推荐指数
3
解决办法
5万
查看次数

在Visual Studio 2010 Express中自动从dll生成C#包装类?

我的一位同事告诉我,Visual Studio允许指向一个.dll并自动神奇地生成一个C#包装类.这真的有可能吗?如果是这样,那么如何实现这一目标呢?我浏览过网络,但未能提出任何建议!

谢谢大家!


想我也会分享这些资源,

c# dll wrapper visual-studio-express

25
推荐指数
2
解决办法
3万
查看次数

Eclipse Indigo中缺少"Servers"视图

我正在尝试使用Eclipse Indigo配置Tomcat,但缺少"Servers"视图.话虽这么说,我必须遵循教程,了解如何使用插件使用Eclipse Indigo配置Tomcat.插件工作,但我想找出为什么首先缺少"服务器"视图.

Eclipse规范:

Version: 3.7.1.r37x_v20110729-9gF7UHOxFtniV7mI3T556iZN9AU8bEZ1lHMcVK
Build id: M20110909-1335
Run Code Online (Sandbox Code Playgroud)

Tomcat规格:

Server version: Apache Tomcat/7.0.22
Server built: Sep 27 2011 09:40:50
Server number: 7.0.22.0
OS Name: Windows XP
OS Version: 5.1
Architecture: x86
JVM Version: 1.6.0_20-b02
Run Code Online (Sandbox Code Playgroud)

操作系统规格:

Microsoft Windows XP
Professional
Version 2002
Service Pack 3
Run Code Online (Sandbox Code Playgroud)

如果重要,是我最初关注的教程.

eclipse apache tomcat

24
推荐指数
3
解决办法
8万
查看次数

如何删除JFrame中的标题栏

我正在使用以下代码进行练习,

http://docs.oracle.com/javase/tutorial/uiswing/examples/layout/BorderLayoutDemoProject/src/layout/BorderLayoutDemo.java

我还补充道

frame.setSize(frame.getMaximumSize());
Run Code Online (Sandbox Code Playgroud)

在createAndShowGUI()方法中,

更重要的是我希望这个窗口没有标题栏,关闭和最小化按钮.

我尝试了以下代码,

frame.setUndecorated(true);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Run Code Online (Sandbox Code Playgroud)

如果我在pack()之前添加了这段代码,它会进入infine循环,这个异常在线程"AWT-EventQueue-0"中有异常java.lang.NegativeArraySizeException

如果我添加了createAndShowGUI()方法的最后一行,它会在线程"AWT-EventQueue-0"中抛出异常java.awt.IllegalComponentStateException:该框架是可显示的.

我该怎么办 ?

谢谢.

java swing titlebar jframe

22
推荐指数
2
解决办法
6万
查看次数

请正确初始化log4j系统警告

这是错误信息 -

log4j:WARN No appenders could be found for logger (SerialPortUtil).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Run Code Online (Sandbox Code Playgroud)

这是SerialPortUtil课堂上的调用-

private static final Logger log = Logger.getLogger(SerialPortUtil.class.getSimpleName());
.
.
.
log.info("Serial port " + port.getName() + " is available");
Run Code Online (Sandbox Code Playgroud)

这是我的log4j.properties文件的内容 -

log4j.rootLogger=DebugAppender

#Debug logging
log4j.appender.DebugAppender=org.apache.log4j.RollingFileAppender
log4j.appender.DebugAppender.Threshold=DEBUG
log4j.appender.DebugAppender.File=activityLog.log
log4j.appender.DebugAppender.MaxFileSize=200KB
log4j.appender.DebugAppender.MaxBackupIndex=5
log4j.appender.DebugAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.DebugAppender.layout.ConversionPattern=%d{DATE} %t - %m%n
Run Code Online (Sandbox Code Playgroud)

属性文件位于类路径中.

一切看起来都对我,所以发生了什么?DEBUG阈值是否也应该捕获INFO日志记录?

java log4j

22
推荐指数
4
解决办法
12万
查看次数

在Java中更改重写方法的访问修饰符?

有没有理由可以更改被覆盖方法的访问修饰符?例如,

abstract class Foo{
    void start(){...}
}
Run Code Online (Sandbox Code Playgroud)

然后将package-private访问修饰符更改为public,

final class Bar extends Foo{
    @Override
    public void start(){...}
}
Run Code Online (Sandbox Code Playgroud)

我只是出于好奇而问这个问题.

java overriding access-modifiers

19
推荐指数
3
解决办法
2万
查看次数

如何有效地取消定期的ScheduledExecutorService任务

因此,使用链接作为参考,是否有人可以建议更优雅的解决方案来取消定期的ScheduledExecutorService任务?

这是我目前正在做的一个例子:

// do stuff

// Schedule periodic task
currentTask = exec.scheduleAtFixedRate(
                            new RequestProgressRunnable(),
                            0, 
                            5000,
                            TimeUnit.MILLISECONDS);

// Runnable
private class RequestProgressRunnable implements Runnable
{
        // Field members
        private Integer progressValue = 0;

        @Override
        public void run()
        {
            // do stuff

            // Check progress value
            if (progressValue == 100)
            {
                // Cancel task
                getFuture().cancel(true);
            }
            else
            {
                // Increment progress value
                progressValue += 10;
            }
        }
    }

    /**
     * Gets the future object of the scheduled task
     * …
Run Code Online (Sandbox Code Playgroud)

java multithreading

18
推荐指数
1
解决办法
1万
查看次数

如何逐行阅读String Builder

我可以逐行阅读String Builder吗?并获得每一行的长度.


编辑:

" 我在StringBuilder中构建字符串并在其中添加"\n".我需要再次阅读它.我需要考虑每个"\n"都有一个新行. "

java string io

18
推荐指数
2
解决办法
6万
查看次数

Eclipse Gradle STS扩展:无法为对象堆保留足够的空间

有一段时间,当Gradle STS扩展程序在启动安装了Gradle STS扩展的Eclipse 3.7(Indigo)后尝试执行我的项目的gradle构建脚本时,我将收到以下错误,

Unable to start the daemon process. The exit value was: 1.
This problem might be caused by incorrect configuration of the daemon.
For example, an unrecognized jvm option is used.
Please refer to the user guide chapter on the daemon at http://gradle.org/docs/current/userguide/gradle_daemon.html
Please read below process output to find out more:
-----------------------
Error occurred during initialization of VM
Could not reserve enough space for object heap
Error: Could not create the Java Virtual …
Run Code Online (Sandbox Code Playgroud)

java eclipse jvm gradle sts-springsourcetoolsuite

18
推荐指数
3
解决办法
1万
查看次数