我有以下枚举:
public enum Rank{
FIRST,
SECOND
}
Run Code Online (Sandbox Code Playgroud)
每当toString()在枚举上调用一个函数(或在字符串文字中使用它)时,我都希望返回FIRST_RANK(而不是字符串' FIRST ',默认情况下).如果我重写toString()函数,如:
@Override
public String toString(){
return this.toString() + "_RANK";
}
Run Code Online (Sandbox Code Playgroud)
但这显然会导致递归.问题是我需要使用toString()枚举的默认实现,并且没有toString()我可以调用的超类枚举.
我正在尝试解析谷歌地图搜索中的JSON数据.我已经尝试了JACKSON和现在我正在尝试JSON SIMPLE.它们都给出了同样的错误.
首先,我正在Google地图上进行搜索.
String urlString = "http://maps.google.com/maps?f=q&source=s_q&output=json&start=0&q="+ "Stockholm" + "+Gym";
Run Code Online (Sandbox Code Playgroud)
给我JSON while(1); {title:"stockholm Gym - Google Maps",url:"/ maps?f = q\x26source = s_q\x26start = 0\x26q = stockholm + Gym\x26ie = UTF8\x26hq = Gym ...............依此类推.我正在替换while(1);用"";在我返回字符串之前.
当我试图解析它时的问题
JSONParser parser = new JSONParser();
String jsonString = "";
Run Code Online (Sandbox Code Playgroud)
// UriHandler.mapSearchJson是返回jsonString的方法.
String jsonData = UriHandler.mapSearchJSON(jsonString);
Object obj = "";
try {
obj = parser.parse(jsonData);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JSONObject jsonObj = (JSONObject) obj;
String title = (String) jsonObj.get("title"); …Run Code Online (Sandbox Code Playgroud) 两者之间有区别吗?
class MyThread extends Thread
{
}
MyThread obj = new MyThread();
Thread t = new Thread(obj);
t.start()
Run Code Online (Sandbox Code Playgroud)
VS
obj.start()
Run Code Online (Sandbox Code Playgroud)
选择一个优于其他优势是否有任何优势?
我有一个方法findNearest,用于查找距离对象最近Object的点ArrayList.
但是我希望能够为不同类的ArrayLists调用方法,这些类是我的基类的超类 Entity
例如,Tree是一个子类Entity,按原样Worker.我希望能够findNearest(x,y,<Arraylist of Trees>)
和也findNearest(x,y,<Arraylist of Workers>)
我想我可以使用一个接口来实现这一点,但是有更简单/更清晰的方法吗?
谢谢
邓肯
我想在多个文件中找到特定的出现模式,并需要生成报告.例如,如果有两个文件说test1.txt和text2.txt.我将不得不浏览文件并在每个文件中查看"samplePattern"的计数.并创建一个类似的报告
<filename> <count>
text1.txt 3
text2.txt 10
Run Code Online (Sandbox Code Playgroud)
我在拥有所有文件的目录中使用了以下命令:
grep -lr -o 'samplePattern' | wc -l
Run Code Online (Sandbox Code Playgroud)
但这给了我所有文件中所有'samplePattern'的累积计数.
使用接口作为类型来实例化类对象有些困惑.为什么我们需要将接口作为一种类型?什么时候我们可以使用这个类?因为该类实现了接口.我知道你无法实例化一个接口.
非常感谢任何建议,
public interface SortRecords {
int query(int rowId);
}
public class OurPrice implements SortRecords {
@Override
public int query(int rowId) {
return 0;
}
}
public void test() {
/*
* Why has the interface as the type?
*/
SortRecords sorterV1 = new OurPrice();
sorterV1.query(1);
/*
* Why not just do like this?
*/
OurPrice sorterV2 = new OurPrice();
sorterV2.query(2);
}
Run Code Online (Sandbox Code Playgroud) 在我的代码中,我想检查Dog的状态object 是否不是 3的状态Enums:
if((dogList.get(i).getStatus()!= dogStatus.SLEEPING
|| dogList.get(i).getStatus()!= dogStatus.WALKING
||dogList.get(i).getStatus()!= dogStatus.EATING )){
//do something
}
Run Code Online (Sandbox Code Playgroud)
这是结合3个||条件的正确方法吗?我问,因为我的程序没有按照我的预期行事.
太多次我必须使用对象列表而不是对象,如果我使用
MyObj a = null;
a.setParameter = 0;
Run Code Online (Sandbox Code Playgroud)
有用.但如果我使用
List<MyObj> a = null;
MyObj b = getObjectFromSomewhere();
a.add(b);
Run Code Online (Sandbox Code Playgroud)
它失败并出现错误:java.lang.NullPointerException?
我怎么看?如果我的新List<MyObj> a单词是"null"或内部没有任何元素?
public class testtype
{
private int a;
private double b;
testtype(int a,double b)
{
this.a=a;
this.b=b;
}
public void maketoequal(testtype oo)
{
oo.a=this.a;
oo.b=this.b;
}
void trytoequal(int c)
{
c=this.a;
}
public static void main(String[] args)
{
testtype t1,t2;
t1=new testtype(10,15.0);
t2=new testtype(5,100.0);
t1.maketoequal(t2);
System.out.println("after the method is called:"+"\n"+"the value of a for t2 is:"+t2.a
+"\n"+"the value of b for t2 is :"+t2.b);
int c=50;
t1.trytoequal(c);
System.out.println("the value of c after the method be called is:"+c);
}
}
Run Code Online (Sandbox Code Playgroud)
为什么c没有改变?
我在java SE 7中创建了一个简单的JDBC程序,但是在编译程序之后,它给出了"java.lang.ClassNotFoundException:org.postgreasql.Driver"错误,我按照这个链接http://docs.oracle.com/javase/7/ docs/technotes/guides/jdbc /但尚未提供,请帮忙.
我使用以下代码使用socket而不是url.openconnection()从指定的url下载文件; 下载后我检查它不工作...当我用编辑器打开文件时它是完全空白的文件内没有数据(空文件)需要建议??? ... ...
try {
String address="http://tineye.com/images/widgets/mona.jpg";
URL url_of_file=new URL(addres);
String hostaddress=url_of_file.getHost();
Socket mysocket=new Socket(hostaddress, 80);
System.out.println("Socket opened to " + hostaddress + "\n");
String file=url_of_file.getFile();
System.out.println(" file = "+file);
OutputStreamWriter osw=new OutputStreamWriter(mysocket.getOutputStream());
osw.write("GET " + file + " HTTP/1.0\r\n\n");
osw.flush();
dis = new DataInputStream(mysocket.getInputStream());
fileData = new byte[7850];
for (int x = 0; fileData[x] > 0; x++){
fileData[x] = (byte) dis.read();
}
// close the data input stream
fos = new FileOutputStream(new File("C:\\Users\\down-to\\filedownloaded.jgp")); //create an object representing the …Run Code Online (Sandbox Code Playgroud) java ×10
enums ×2
arraylist ×1
grep ×1
if-statement ×1
inputstream ×1
interface ×1
jdbc ×1
json ×1
linux ×1
list ×1
overriding ×1
parsing ×1
shell ×1
sockets ×1