这是我的代码片段:
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编译器抛出错误说
错误:变量someObject可能尚未初始化.
但是,如果success等于true,则没有办法someObject不会被初始化,为什么我会收到此错误?
这是代码:
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) 这来自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环境:
这是片段:
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) 这是源代码:
删除此列表中指定位置的元素.将任何后续元素向左移位(从索引中减去一个).参数: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)?
这是代码:
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) 以下是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)