小编Sta*_*sky的帖子

Java:int数组使用非零元素初始化

根据JLS,int在初始化之后,应该用零填充数组.但是,我遇到的情况并非如此.这种行为首先发生在JDK 7u4中,也发生在所有后续更新中(我使用64位实现).以下代码抛出异常:

public static void main(String[] args) {
        int[] a;
        int n = 0;
        for (int i = 0; i < 100000000; ++i) {
            a = new int[10];
            for (int f : a)
                if (f != 0)
                  throw new RuntimeException("Array just after allocation: "+ Arrays.toString(a));
            Arrays.fill(a, 0);
            for (int j = 0; j < a.length; ++j)
                a[j] = (n - j)*i;
            for (int f : a)
                n += f;
        }
        System.out.println(n);
    }
Run Code Online (Sandbox Code Playgroud)

在JVM执行代码块的编译之后发生异常,并且不会出现-Xint标志.此外,该Arrays.fill(...)语句(与此代码中的所有其他语句一样)是必要的,如果不存在则不会发生异常.很明显,这个可能的错误与一些JVM优化有关.出于这种行为的原因有什么想法吗?

更新: …

java arrays jvm initialization java-7

130
推荐指数
1
解决办法
6937
查看次数

Spring MVC InterceptorHandler使用DeferredResult调用了两次

当我使用自定义HandlerInterceptor并且我的控制器返回时DeferredResult,preHandle我的自定义拦截器的 方法在每个请求上调用两次.考虑一个玩具示例.

我的自定义拦截器:

public class MyInterceptor implements HandlerInterceptor {
    static int i = 0;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println(i++);
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
    }
}
Run Code Online (Sandbox Code Playgroud)

我的Spring Java配置:

@Configuration
@EnableWebMvc
public class ApplicationConfiguration extends WebMvcConfigurerAdapter {

    @Override
    public void …
Run Code Online (Sandbox Code Playgroud)

java spring-mvc

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

Groovy 覆盖 compareTo

我正在使用 Groovy 类别的 DSL 下工作,我需要覆盖/重载==运算符。然而,已知的问题是,当类实现时Comparable,Groovy 将调用操作符的compareTo()方法==。我正在寻找一些解决方法(不是 AST 转换),以便==完全按照我的意愿行事。

我有以下“玩具”情况:

class Base implements Comparable<Base>{
    int a, b

    Base(int a, int b) {
        this.a = a
        this.b = b
    }

    @Override
    int compareTo(Base o) {
        return a <=> o.a //compare only a
    }
}

class BaseCategory {
    static boolean equals(Base base, o) { //complete equals
        if (o == null)
            throw new NullPointerException()
        if (o.getClass() != base.getClass())
            return false
        return base.a …
Run Code Online (Sandbox Code Playgroud)

dsl groovy

6
推荐指数
1
解决办法
2928
查看次数

SwiftUI:在“列表”编辑模式下更改删除按钮标题

有没有办法在使用内置编辑列表时指定删除按钮标题DeleteButton

示例代码:

struct ExampleView: View {
    @State
    private var users = ["Paul", "Taylor", "Adele"]

    var body: some View {
        NavigationView {
            List {
                ForEach(users, id: \.self) { user in
                    Text(user)
                }.onDelete(perform: delete)
            }.navigationBarItems(trailing: EditButton())
        }
    }

    func delete(source: IndexSet) { }
}
Run Code Online (Sandbox Code Playgroud)

代码给出(当编辑处于活动状态时):

默认删除按钮:

我想放置“some_text”而不是“Delete”——SwiftUI 可以吗?另外,是否可以更改一行的删除标题?

swiftui

6
推荐指数
0
解决办法
480
查看次数

Java中的置换组实现

根据我在编程方面的经验,我经常面对与排列组相关的不同任务:枚举给定排列的所有可能产品或只计算它们,测试一个排列是否可以表示为给定排列的组合,在给定组中找到一个子组我认为这些问题是计算机科学的经典问题,并且出现在各种编程领域.目前,在我们的项目中,我们使用PermutationGroup基于最简单版本的Schreier-Sims算法的原始实现,但它非常有限.我了解各种C++和Python库,但是有没有任何Java库可以有效地实现PermutationGroup相关主题?

谢谢,斯坦尼斯拉夫.

java permutation symmetry

5
推荐指数
1
解决办法
1222
查看次数

在 groovy shell 中使用 groovy 类别

我正在使用 Groovy 类别在一些 DSL 下工作,我想找到一种方法来将我的 DSL 与 groovy shell 一起使用,而无需use(MyCategory){ myObject.doSomething() }为每个命令显式编写。

例如,假设我有以下玩具类别:

class MyCategory {
    static Integer plus(Integer integer, String string){
        return integer + Integer.valueOf(string)
    }
}
Run Code Online (Sandbox Code Playgroud)

然后,我可以通过groovysh以下方式使用此类别:

groovy> use(MyCategory){ 2 + '3' } //gives 5
Run Code Online (Sandbox Code Playgroud)

那么,有没有办法MyCategory为所有groovysh命令全局设置,这样就没有必要每次都将我的命令包装在use(MyCategory) { ... }?例如:

groovy> useGlobally(MyCategory); //call something like this only once
groovy> 2 + '3' //automatically uses MyCategory and gives 5
Run Code Online (Sandbox Code Playgroud)

dsl groovy groovyshell

5
推荐指数
1
解决办法
216
查看次数

Scala无法推断Java方法的类型参数

我在Java中有以下复杂的类型层次结构:

// the first type
interface Element<Type extends Element<Type>> {
    Type foo(Type a, Type b);
}

// the second type
interface Payload<Type extends Payload<Type>> {
    Type bar(Type[] array);
}

// some toy implementation
final class SomePayload implements Payload<SomePayload> {
    @Override
    public SomePayload bar(SomePayload[] array) { return array[0]; }
}

// mix of first and second interfaces
interface ComplicatedElement<
              PayloadT extends Payload<PayloadT>,
              ObjectT extends ComplicatedElement<PayloadT, ObjectT>>
          extends Element<ObjectT> {

    PayloadT getPayload();

    ObjectT add(ObjectT a, ObjectT b);
}

// some toy implementation …
Run Code Online (Sandbox Code Playgroud)

types scala

5
推荐指数
1
解决办法
358
查看次数

SwiftUI:将绑定转换为另一个绑定

有没有办法,例如逻辑否定Binding<Bool>?例如,我有一个状态变量

@State var isDone = true
Run Code Online (Sandbox Code Playgroud)

我将其作为投标传递给不同的子视图。然后我想将它与isActivein一起使用NavigationLink,以便它仅在not isDone以下情况下显示:

NavigationLink(destination: ..., isActive: ! self.$isDone ) // <- `!` means `not done`
Run Code Online (Sandbox Code Playgroud)

当然,我可以用 反转我的逻辑isDone -> isNotDone,但在许多情况下这将是不自然的。那么有没有简单的方法来反转 bool 绑定?

swiftui

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