小编San*_*jee的帖子

为什么 Java 程序不终止

下面的代码检查ExecutorCompletionServicefromJava Concurrency框架的使用(正在使用的 IDE 是 Netbeans)。

但程序不会终止。为什么?

代码:

import java.util.concurrent.Callable;
import java.util.concurrent.CompletionService;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.Executors;

public class TestFuture {

    public static void main(String... args) throws InterruptedException, ExecutionException {
        Executor ex = Executors.newCachedThreadPool();
        CompletionService<Long> cs = new ExecutorCompletionService<Long>(ex);
        cs.submit(new Worker());
        cs.submit(new Worker());
        cs.submit(new Worker());
        for (int i = 0; i < 3; i++) {
            long l = cs.take().get();
            //utilize the result
            System.out.println(l);
        }
    }
}

class Worker implements Callable {

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

java concurrency future executors

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

Java Formatter输出

格式化程序只在关闭流时才会写出来吗?

码:

public void save(String content, String filename) {
    if(filename!=""){
        setFileName(filename);
        try {
            output = new Formatter(new File(fileName));
            output.format("%s",content);
        } catch (FormatterClosedException ex){
            System.err.println("Error writing to file");
            return;
        } catch ( NoSuchElementException ex){
            System.err.println("invalid input");
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        finally {
            output.close();
        }
Run Code Online (Sandbox Code Playgroud)

起初我忘了用output.close()添加finally块,当调用该方法时,它不会写任何东西.那么格式化程序在关闭流时只实际写入其内容是否正确?

java formatter

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

拖动TreeView的节点

使用此示例我想创建带有节点的TreeView,我可以拖动它以更改其位置.

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.TreeCell;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeItem.TreeModificationEvent;
import javafx.scene.control.TreeView;
import javafx.scene.input.ClipboardContent;
import javafx.scene.input.DataFormat;
import javafx.scene.input.DragEvent;
import javafx.scene.input.Dragboard;
import javafx.scene.input.MouseEvent;
import javafx.scene.input.TransferMode;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Callback;

public class MainApp extends Application {

    private EventHandler<TreeModificationEvent<DynamicTreeNodeModel>> branchExpandedEventHandler;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Application.launch(MainApp.class, args);
    }

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Example Dynamic Tree");
        primaryStage.setResizable(true);
        final VBox …
Run Code Online (Sandbox Code Playgroud)

java javafx javafx-2 javafx-8

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

Parse .txt to .csv

Is it possible to create a Java program which recognizes the text in a .txt file and write it in a .csv file? If yes,how would you start with such a problem?

My .txt file is Text1 |Text 2 so I could somehow get the char "|" and split it into two cells.

java csv parsing

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

如何通过在java中传递当前日期来获取下一个日期

如何在Java中显示当前日期(2014/03/20)的下一个日期(2014/03/21)?

码:

 public static String getNextDate(String  curDate) {
         String nextDate="";
        try {
           //here logic to get nextDate
        } catch (Exception e) {
            return nextDate;
        }
        return nextDate;
    }
Run Code Online (Sandbox Code Playgroud)

java date

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

比较java android中的字符串

我如何比较字符串

http://192.168.74.1/sp/info.php?prodnum=0000000001
Run Code Online (Sandbox Code Playgroud)

http://192.168.74.1
Run Code Online (Sandbox Code Playgroud)

我只需要得到http://192.168.74.1第一个字符串的 一部分.

java string android string-comparison

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

将项添加到列表时java.lang.StackOverflowError

尝试将项目添加到列表并打印它们,它编译,但我得到一个运行时错误,其中堆栈溢出错误.这是错误打印出来的:

Exception in thread "main" java.lang.StackOverflowError
at List.<init>(List.java:5)
at List.<init>(List.java:9)
at List.<init>(List.java:9) <----- this line is repeated quite a few times 
Run Code Online (Sandbox Code Playgroud)

这是我的代码,包含添加和打印列表的方法.

public class List {

private AthleteNode front;

public List(){
front = null;
}

public List athletes = new List();

//add athlete to the end of the list 
public void add(Athlete a){

AthleteNode node = new AthleteNode (a);
AthleteNode current; //temp node to iterate over the list

if(front == null)
    front = node;//adds the first element

else{
    current = …
Run Code Online (Sandbox Code Playgroud)

java stack-overflow

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

使用SpEL创建Calendar对象

给定a java.util.Date(),如何使用Spring Expression Language创建Calendar对象?

这个工作: <property name="calendarObject" value="#{new java.util.GregorianCalendar()}"/>

但是我需要从java.util.Date()我拥有的日期开始为它的构造函数提供日,月和年.我想使用该java.util.Date().getDay()方法,但显然它已被弃用.

我尝试使用Calendar.setTime()方法但它不起作用,因为它的返回类型是void.

java calendar spring-el

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

如何对数组进行排序而忽略大小写?

我使用sort()按字母顺序对数组进行排序,但它从AZ到az进行排序.我尝试事先将每个单词大写,但除非打印出来,否则它不起作用,这应该在排序后发生.目前,使用此代码,它将使用大写字母列出学生,但如果它以小写形式输入,则将其排序为小写.在将输入分配给数组之后,将capitalize()放在初始for循环中是行不通的.有解决方案吗

import java.util.Scanner;
import java.util.Arrays;

public class Pupils {
public static void main(String[] args) {

    Scanner scan = new Scanner(System.in);
    boolean loop = true;
    int names = 0;

    String[] ay = new String[1000];

    for(int i = 0; loop == true; i++) {
            System.out.println("Enter name: ");
            ay[i] = scan.nextLine();
            names++;
            if (ay[i].equals("0")) {
                loop = false;
                ay[i] = " ";
        }
    }

    String[] aay = new String[names - 1];

    for(int i = 0; i < aay.length; i++) {
        aay[i] = ay[i]; …
Run Code Online (Sandbox Code Playgroud)

java arrays sorting

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

如何根据poja类值删除列表中的项目

如何从列表中删除列表项,如下所示

List<Mypojaclass> results = new ArrayList<>();
      // in the results i am adding more items using sql query, after that,
        if(Mypojaclass sensor : results){
            if(sensor.getType.equals("JD"){
             sensor.remove(sensor);
             }
        }
Run Code Online (Sandbox Code Playgroud)

我可以使用迭代器,如下所示,

List<String> results = new ArrayList<>();
for (Iterator<String> iter = list.listIterator(); iter.hasNext(); ) {
    String a = iter.next();
    if (...) {
        iter.remove();
    }
}
Run Code Online (Sandbox Code Playgroud)

但我应该删除基于该sensor.getType的项目.

我怎样才能实现这一目标?

java list

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