小编use*_*610的帖子

编译器说"变量可能没有被初始化",虽然我有一个标志变量来保证它

这是我的代码片段:

Someclass someObject;
boolean success = true;
try {
    someObject = someOperation();
} catch (Exception e) {
    success = false;
}
if (success) {
    int number = Integer.valueOf(someObject.someMethord());
}
Run Code Online (Sandbox Code Playgroud)

并在行内:

 int number = Integer.valueOf(someObject.someMethord());
Run Code Online (Sandbox Code Playgroud)

Java编译器抛出错误说

错误:变量some​​Object可能尚未初始化.

但是,如果success等于true,则没有办法someObject不会被初始化,为什么我会收到此错误?

java

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

我可以使用Collection.size()替换此代码中的计数器吗?

这是代码:

public class LogService {
    private final BlockingQueue<String> queue;
    private final LoggerThread loggerThread;
    private final PrintWriter writer;
    @GuardedBy("this") private boolean isShutdown;
    @GuardedBy("this") private int reservations;    //  <-- counter
    public void start() { loggerThread.start(); }
    public void stop() {
        synchronized (this) { isShutdown = true; }
        loggerThread.interrupt();
    }
    public void log(String msg) throws InterruptedException {
        synchronized (this) {
            if (isShutdown)
                throw new IllegalStateException(...);
            ++reservations;
        }
        queue.put(msg);
    }
    private class LoggerThread extends Thread {
        public void run() {
            try {
                while (true) …
Run Code Online (Sandbox Code Playgroud)

java concurrency multithreading

8
推荐指数
1
解决办法
76
查看次数

为什么此代码中没有类型转换异常?

这来自Thinking in Java

class Snow {}
class Powder extends Snow {}
class Light extends Powder {}
class Heavy extends Powder {}
class Crusty extends Snow {}
class Slush extends Snow {}

public class AsListInference {
    public static void main(String[] args) {
        //The book says it won't compile, but actually it does.
        List<Snow> snow2 = Arrays.asList(new Light(), new Heavy());
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的Java环境:

  1. java版"1.8.0_60"
  2. Java(TM)SE运行时环境(版本1.8.0_60-b27)
  3. Java HotSpot(TM)64位服务器VM(内置25.60-b23,混合模式)

java

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

这个中断()是否必要?

这是片段:

public class LogService {

    public void stop() {
        synchronized (this) { isShutdown = true; }
        loggerThread.interrupt();    /* Is it necesarry? */
    }

    public void log(String msg) throws InterruptedException {
        synchronized (this) {
            if (isShutdown)
            throw new IllegalStateException(...);
            ++reservations;
        }
        queue.put(msg);
    }

    private class LoggerThread extends Thread {
        public void run() {
            try {
                while (true) {
                    try {
                        synchronized (LogService.this) {
                            if (isShutdown && reservations == 0)
                                break;
                        }
                        String msg = queue.take();
                        synchronized (LogService.this) {
                        --reservations;
                        }
                        writer.println(msg); …
Run Code Online (Sandbox Code Playgroud)

java multithreading

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

这段代码是否在Arraylist.remove(int index)的源代码中是不必要的?

这是源代码:

删除此列表中指定位置的元素.将任何后续元素向左移位(从索引中减去一个).参数:index要删除的元素的索引返回:从列表中删除的元素抛出: java.lang.IndexOutOfBoundsException

public E remove(int index) {
    rangeCheck(index);
    modCount++;
    E oldValue = elementData(index);
    int numMoved = size - index - 1;
    if (numMoved > 0)
    System.arraycopy(elementData, index+1, elementData, index,
        numMoved);
    elementData[--size] = null; // Let gc do its work
    return oldValue;
}
Run Code Online (Sandbox Code Playgroud)

我的问题是:

由于rangeCheck(索引)已经保证索引<size,是否有必要检查if (numMoved > 0)

java arraylist

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

为什么我的快速排序不能在Java中实现?

这是代码:

public class QuickSort {

    public static void sort(int[] a) {
        sort(a, 0, a.length-1);
    }

    private static void sort(int[] a, int lo, int hi) {
        if(hi <= lo) return;
        int j = partition(a, lo, hi);
        sort(a, lo, j-1);
        sort(a, j+1, hi);
    }

    private static int partition(int[] a, int lo, int hi) {
        int v = a[0];
        int i = lo, j = hi+1;
        while(true) {
            while(a[++i] < v) {
                if(i == hi) {
                    break;
                }
            }
            while(a[--j] > v) { …
Run Code Online (Sandbox Code Playgroud)

java sorting algorithm quicksort

0
推荐指数
1
解决办法
101
查看次数

if(str == NULL || str.length()== 0)得到错误

以下是leetcode算法问题的代码:

class Solution {
public:
    int myAtoi(string str) {
        if(str == NULL || str.length() == 0) return 0;
        int pos = true;
        int result = 0;
        int i = 0;
        if(str.charAt(0) == '+' || str.charAt(0) == '-') {
            ++i;
            if(str.charAt(0) == '-') pos = false;
        }
        for(; i != str.length(); ++i) {
            if(str.charAt(i) >= '0' && str.charAt(i) <= '9') {
                result = result*10 + (int)(str.charAt(i)-'0');
            }
        }
        if(!pos) result=-result;
        if(result > INT_MAX) return INT_MAX;
        if(result < INT_MIN) return INT_MIN;
        return …
Run Code Online (Sandbox Code Playgroud)

c++ atoi

-3
推荐指数
2
解决办法
262
查看次数