小编dfa*_*dfa的帖子

为什么数组不能分配给Iterable?

用Java5我们可以写:

Foo[] foos = ...
for (Foo foo : foos) 
Run Code Online (Sandbox Code Playgroud)

或者只是在for循环中使用Iterable.这非常方便.

但是你不能像这样为iterable编写泛型方法:

public void bar(Iterable<Foo> foos) { .. }
Run Code Online (Sandbox Code Playgroud)

并使用数组调用它,因为它不是Iterable:

Foo[] foos = { .. };
bar(foos);  // compile time error 
Run Code Online (Sandbox Code Playgroud)

我想知道这个设计决定背后的原因.

java language-design

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

在提交之前添加POST参数

我有这个简单的形式:

<form id="commentForm" method="POST" action="api/comment">
    <input type="text" name="name" title="Your name"/>
    <textarea  cols="40" rows="10" name="comment" title="Enter a comment">
    </textarea>
    <input type="submit" value="Post"/>
    <input type="reset" value="Reset"/>
</form>
Run Code Online (Sandbox Code Playgroud)

我需要在发送到服务器之前添加两个POST参数:

var params = [
               {
                 name: "url",
                 value: window.location.pathname
               },
               {
                  name: "time",
                  value: new Date().getTime()
               }
             ];
Run Code Online (Sandbox Code Playgroud)

请不要修改表格.

javascript forms jquery

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

使用String.format()打印空格

我怎么能改写这个:

for (int i = 0; i < numberOfSpaces; i++) {
    System.out.print(" ");
}
Run Code Online (Sandbox Code Playgroud)

String.format()

PS

我很确定这是可能的,但javadoc有点令人困惑.

java string format

47
推荐指数
2
解决办法
10万
查看次数

如何说服你的开发人员编写简短的方法?

长期方法在某些方面是邪恶的:

  • 他们很难理解
  • 他们很难改变
  • 它们很难重复使用
  • 他们很难测试
  • 他们的凝聚力很低
  • 它们可能具有高耦合
  • 他们往往过于复杂

如何说服你的开发人员编写简短的方法?(武器被禁止=)

来自agiledeveloper的问题

language-agnostic metrics

33
推荐指数
8
解决办法
2347
查看次数

在创建文件内部之前检查目录中的写访问权限

我的小实用程序应用程序通过GUI文件选择器向用户询问输出目录.然后,经过一些处理后,它会在此输出目录中创建大量文件.

我需要检查应用程序是否具有写访问权限,以便通知用户并且不继续处理(这可能需要很长时间)

我的第一次尝试是java.io.FilecanWrite()方法.但这不起作用,因为它处理目录条目本身而不是其内容.我已经看到至少一个Windows XP文件夹的实例可以重命名或删除但是不能在其中创建文件(因为权限).这实际上是我的测试用例.

我终于解决了以下解决方案

//User places the input file in a directory and selects it from the GUI
//All output files will be created in the directory that contains the input file
File fileBrowse = chooser.getSelectedFile(); //chooser is a JFileChooser
File sample = new File(fileBrowse.getParent(),"empty.txt"); 
try
{
     /*
      * Create and delete a dummy file in order to check file permissions. Maybe 
      * there is a safer way for this check.
      */
      sample.createNewFile();
      sample.delete();
} …
Run Code Online (Sandbox Code Playgroud)

java permissions file

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

Ant + JUnit:NoClassDefFoundError

好的,我很沮丧!我已经狩猎了好几个小时,我仍然难过.

环境:WinXP,Eclipse Galileo 3.5(直接安装 - 无需额外插件).

所以,我有一个简单的JUnit测试.它从内部Eclipse JUnit运行配置运行良好.这个类没有依赖任何东西.为了尽可能缩小这个问题,它只包含:

@Test
public void testX() {
    assertEquals("1", new Integer(1).toString());
}
Run Code Online (Sandbox Code Playgroud)

到目前为止没有汗水.现在我想采取从Ant内部运行此测试用例的超级高级步骤(最终目标是与Hudson集成).

所以,我创建了一个build.xml:

<project name="Test" default="basic">
    <property name="default.target.dir" value="${basedir}/target" />
    <property name="test.report.dir" value="${default.target.dir}/test-reports" />

    <target name="basic">
        <mkdir dir="${test.report.dir}" />
        <junit fork="true" printSummary="true" showOutput="true">
            <formatter type="plain" />
            <classpath>
                <pathelement path="${basedir}/bin "/>
            </classpath>
            <batchtest fork="true" todir="${test.report.dir}" >
                <fileset dir="${basedir}/bin">
                    <include name="**/*Test.*" />
                </fileset>
            </batchtest>
        </junit>
    </target>
</project>
Run Code Online (Sandbox Code Playgroud)

$ {basedir}是工作空间中包含源,类和构建文件的Java项目名称.所有.java和build.xml都在$ {basedir}/src中..class文件位于$ {basedir}/bin中.

我已经通过Windows/Preferences/Ant/Runtime/Contributed Entries将eclipse-install-dir/plugins/org.junit4_4.5.0.v20090423/junit.jar添加到Ant运行时类路径中.ant-junit.jar在Ant Home Entries中.

那么,当我运行这个疯狂复杂的目标时会发生什么?我的报告文件包含:

Testsuite: com.xyz.test.RussianTest
Tests run: 1, Failures: 0, Errors: 1, Time …
Run Code Online (Sandbox Code Playgroud)

java ant junit classpath hamcrest

26
推荐指数
3
解决办法
4万
查看次数

用于hashCode/equals契约的JUnit理论

以下类用作equals/hashCode契约的通用测试器.它是本土测试框架的一部分.

  • 你有什么想法?
  • 我怎样(强)测试这门课程?
  • 这是Junit理论的一个很好的用途?

班级:

@Ignore
@RunWith(Theories.class)
public abstract class ObjectTest {

    // For any non-null reference value x, x.equals(x) should return true
    @Theory
    public void equalsIsReflexive(Object x) {
        assumeThat(x, is(not(equalTo(null))));
        assertThat(x.equals(x), is(true));
    }

    // For any non-null reference values x and y, x.equals(y) 
    // should return true if and only if y.equals(x) returns true.
    @Theory
    public void equalsIsSymmetric(Object x, Object y) {
        assumeThat(x, is(not(equalTo(null))));
        assumeThat(y, is(not(equalTo(null))));
        assumeThat(y.equals(x), is(true));
        assertThat(x.equals(y), is(true));
    }

    // For any non-null reference values x, y, …
Run Code Online (Sandbox Code Playgroud)

java junit unit-testing

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

Java中的异常驱动编程

我刚刚读完了Exception Driven Programming,我想知道像ELMAH for Java 这样的东西.你知道它吗?

有趣的功能:

  • 用于远程查看重新编码的异常的整个日志的网页
  • 用于远程查看任何一个已记录异常的完整详细信息的网页
  • 每个错误发生时的电子邮件通知
  • 来自日志的最后15个错误的RSS提要
  • 其他接口(JSON,RESTful接口等)
  • 日志的许多后备存储实现,包括内存,JDBC,JMS等
  • 开源

注意

log4j用于日志记录,它不是异常处理的集成解决方案

c# java

21
推荐指数
1
解决办法
2530
查看次数

Java性能计时库

我经常在System.nanoTime()对中包装代码以便对其进行计时.就像是:

long start = System.nanoTime();    
methodToBeTimed();
long elapsedTime = System.nanoTime() - start;
Run Code Online (Sandbox Code Playgroud)

有什么好的计时库可以解决这个问题吗?还将接受自行开发的代码.

NB

分析器不是解决方案,因为我想在单元测试中强制执行一些时间限制,所以我想以编程方式对方法进行计时.

java performance timing

21
推荐指数
4
解决办法
2万
查看次数

你最常用的课程是什么?

一段时间后,每个程序员都会得到一组实用程序类.其中一些是真正的编程珍珠,它们在你的几个项目中被重用.例如,在java中:

 class Separator {

        private String separator;
        private boolean called;

        public Separator(String aSeparator) {
            separator = aSeparator;
            called = false;
        }

        @Override
        public String toString() {
            if (!called) {
                called = true;
                return "";
            } else {
                return separator;
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

和:

public class JoinHelper {

    public static <T> String join(T... elements) {
        return joinArray(" ", elements);
    }

    public static <T> String join(String separator, T... elements) {
        return joinArray(separator, elements);
    }

    private static <T> String joinArray(String sep, T[] elements) …
Run Code Online (Sandbox Code Playgroud)

language-agnostic oop

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