小编mar*_*hon的帖子

如何防止java.lang.String.split()创建一个前导空字符串?

传递0作为限制参数可以防止尾随空字符串,但是如何防止前导空字符串?

例如

String[] test = "/Test/Stuff".split("/");
Run Code Online (Sandbox Code Playgroud)

产生一个带有"","测试","东西"的数组.

是的,我知道我可以推出自己的Tokenizer ...但是StringTokenizer的API文档说

"StringTokenizer是一个遗留类,出于兼容性原因而保留,但在新代码中不鼓励使用它.建议任何寻求此功能的人都使用拆分"

java string

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

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

Http Post请求内容类型表单在Spring MVC 3中不起作用

代码段:

@RequestMapping(method = RequestMethod.POST)//,  headers = "content-type=application/x-www-form-urlencoded")
public ModelAndView create(@RequestBody UserAccountBean account) {
    try{
        accounts.put(account.assignId(), account);
    }catch(RuntimeException ex)
    {
        return new ModelAndView("account/registerError");
    }
    return new ModelAndView("account/userVerification");
}
Run Code Online (Sandbox Code Playgroud)

收到请求后,我得到的是Http状态代码415:服务器拒绝了此请求,因为请求实体的格式不受所请求方法所请求资源的支持().

如果我将代码更改为:

代码段:

@RequestMapping(method = RequestMethod.POST,headers = "content-type=application/x-www-form-urlencoded")
public ModelAndView create(@RequestBody UserAccountBean account) {
    try{
        accounts.put(account.assignId(), account);
    }catch(RuntimeException ex)
    {
        return new ModelAndView("account/registerError");
    }
    return new ModelAndView("account/userVerification");
}
Run Code Online (Sandbox Code Playgroud)

我将不允许405方法.有趣的是在允许的响应标题中,它将GET和POST列为允许的方法.

我有一个做JOSN映射的类:

@Component
public class JacksonConversionServiceConfigurer implements BeanPostProcessor {

private final ConversionService conversionService;

@Autowired
public JacksonConversionServiceConfigurer(ConversionService conversionService) {
    this.conversionService = conversionService;
}

public Object postProcessBeforeInitialization(Object …
Run Code Online (Sandbox Code Playgroud)

content-type spring-mvc

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

在VS2010中打开类或文件的快捷键?

我已经用Google搜索了答案 - 找不到答案.

在Intellij中,如果你想打开任意一个类,你按ctrl-n,你会得到一个小文本框,你可以在其中键入一个类的名称或只是驼峰名称首字母或一些通配符.

与文件相同 - ctrl-shift-n.

在Visual Studio 2010中执行此操作的击键是什么?

visual-studio-2010

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

java是否有任何机制让VM自己跟踪方法调用,而不使用javaagent等?

我想动态地构建调用图,从任意方法调用开始,或者从运行的JVM本身中使用新线程开始构建调用图.(这个软件将成为负载测试另一个消耗调用图的软件的测试工具)

我知道有一些SPI接口,但看起来你需要用它们运行-javaagent标志.我想直接在VM本身访问它.

理想情况下,我想获得每个方法调用的入口和出口的回调,该方法调用的参数以及该方法中的时间.显然在单个线程内.

我知道AOP可能会这样做,但我只是想知道JDK中是否有工具可以让我捕获它.

java instrumentation trace callstack

19
推荐指数
1
解决办法
6074
查看次数

如何创建完全不可变的树层次结构?建筑鸡肉和鸡蛋

我喜欢使数据类不可变,以使并发编程更容易.但是,制定完全不可变的层次结构似乎存在问题.

考虑这个简单的树类:

public class SOTree {
    private final Set<SOTree> children = new HashSet<>();
    private SOTree parent;

    public SOTree(SOTree parent) {
        this.parent = parent;
    }

    public SOTree(Set<SOTree> children) {
        for (SOTree next : children)
            children.add(next);
    }


    public Set<SOTree> getChildren() {
        return Collections.unmodifiableSet(children);
    }

    public SOTree getParent() {
        return parent;
    }
}
Run Code Online (Sandbox Code Playgroud)

现在,如果我想创建这些层次结构,当我构造它时,父节点必须存在于当前节点之前,或者子节点必须首先存在.

    SOTree root = new SOTree((SOTree)null);
    Set<SOTree> children = createChildrenSomehow(root);
    //how to add children now?  or children to the children?
Run Code Online (Sandbox Code Playgroud)

要么

    Set<SOTree> children = createChildrenSomehow(null);
    SOTree …
Run Code Online (Sandbox Code Playgroud)

java tree immutability data-structures

16
推荐指数
4
解决办法
5784
查看次数

防止toString()中无限递归的最有效方法?

如果收集项的图形中的某个地方是一个返回自身的引用,则对集合上的字符串可以进入无限循环.见下面的例子.

是的,良好的编码实践应该首先防止这种情况,但无论如何,我的问题是:在这种情况下检测递归的最有效方法是什么?

一种方法是在threadlocal中使用一个集合,但这看起来有点沉重.

public class AntiRecusionList<E> extends ArrayList<E> {
  @Override
  public String toString() {
    if (  /* ???? test if "this" has been seen before */ ) {
        return "{skipping recursion}";
    } else {
        return super.toString();
    }
  }
}


public class AntiRecusionListTest {
  @Test
  public void testToString() throws Exception {
      AntiRecusionList<AntiRecusionList> list1 = new AntiRecusionList<>();
      AntiRecusionList<AntiRecusionList> list2 = new AntiRecusionList<>();
      list2.add(list1);
      list1.add(list2);
      list1.toString();  //BOOM !
  }
}
Run Code Online (Sandbox Code Playgroud)

java recursion tostring

16
推荐指数
2
解决办法
8756
查看次数

maven程序集插件模块集源指令不包括任何文件,也不匹配包含的模块

我有一个多模块maven项目,我正在尝试使组件插件的模块集源部分工作.

我有模块" module_parent "," module_a "和" module_assembly ".

module_amodule_assemblymodule_parent的节点.

module_assemblymodule_a上有一个声明的pom依赖.

module_assmebly有程序集插件,assembly.xml看起来像:

<?xml version="1.0"?>
<assembly>
  <id>bin</id>
  <formats>
    <format>zip</format>
  </formats>
  <includeBaseDirectory>false</includeBaseDirectory>
  <moduleSets>
    <moduleSet>
      <useAllReactorProjects>true</useAllReactorProjects>
      <includes>
        <include>com.mystuff:module_a</include>
      </includes>
      <sources>
        <fileSets>
          <fileSet>
            <outputDirectory>/</outputDirectory>
            <excludes>
              <exclude>**/target/**</exclude>
            </excludes>
            <directory>/</directory>
          </fileSet>
        </fileSets>
      </sources>
    </moduleSet>
  </moduleSets>
</assembly> <!-- -->
Run Code Online (Sandbox Code Playgroud)

我对module_a的module_assembly有一个显式依赖.module_assembly的pom中的依赖关系如下:

<dependencies>
    <dependency>
        <groupId>com.mystuff</groupId>
        <artifactId>module_a</artifactId>
        <version>1.0</version>
        <type>pom</type>
    </dependency>
</dependencies>
Run Code Online (Sandbox Code Playgroud)

我打开调试时从mvn包获得的错误是:

[DEBUG] All known ContainerDescritporHandler components: [metaInf-services, plexus, metaInf-spring, file-aggregator]
[DEBUG] No dependency sets specified.
[WARNING] The …
Run Code Online (Sandbox Code Playgroud)

java maven maven-assembly-plugin

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

从搜索文档中查找最小片段的算法?

我一直在阅读Skiena出色的"算法设计手册",并在其中一个练习中被挂了.

问题是:"给定三个单词的搜索字符串,找到包含所有三个搜索词的文档的最小片段 - 即,其中包含最少数量单词的片段.您将获得这些单词的索引位置在出现的搜索字符串中,例如word1:(1,4,5),word2:(4,9,10)和word3:(5,6,15).每个列表按排序顺序排列,如上所述. "

我想出的任何东西都是O(n ^ 2)......这个问题出现在"排序和搜索"一章中,所以我假设有一种简单而聪明的方法.我现在正在尝试使用图表,但这似乎有些过分.

想法?谢谢

algorithm

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

Rust中extern fn和extern"C"fn有什么区别?

我已经尝试阅读各种github问题,试图找出差异是什么,只是最终混淆了.

#[no_mangle]
pub extern fn foo() {
   ...
}
Run Code Online (Sandbox Code Playgroud)

#[no_mangle]
pub extern "C" fn foo() {
   ...
}
Run Code Online (Sandbox Code Playgroud)

ffi rust

11
推荐指数
1
解决办法
1687
查看次数