假设我有一个json数组数组
String jsonString = [["John","25"],["Peter","37"]];
Run Code Online (Sandbox Code Playgroud)
我想把它变成ArrayList<ArrayList<String>>对象.当我用的时候
Gson.fromJson(jsonString,ArrayList<ArrayList<String>>.class)
它似乎没有用,我通过使用做了一个工作
Gson.fromJson(jsonString,String[][].class)
有一个更好的方法吗?
我只是想知道为什么java.util.Scanner实现了java.util.Iterator?
Scanner实现remove方法并抛出UnsupportedOperationException.
但是,在实现接口时,不应该是一个类,履行接口的契约?
实现iterator和添加抛出异常的方法有什么用?
为什么不避免接口的实现并保持简单?
有人可以争辩说,它被定义为可以扩展的类Scanner可以实现该方法,就像AbstractList有一个抛出一个的add方法UnsupportedOperationException.但是AbstractList是一个abstract班级,而是Scanner一个final班级.
这不是一个糟糕的设计实践吗?
以下方法完美无瑕
public <T> void fromJsonArray(String jsonString,Type tToken) {
Gson g = new Gson();
T list = g.fromJson(jsonString,tToken);
System.out.println(list);
}
Run Code Online (Sandbox Code Playgroud)
但是我还没有说明这种方法中的<T>是什么.如何将编译器返回的值分配给i未指定fromJson的变量list?
我刚刚测试了答案的有效性,说明<T>是从方法的返回类型推断出来的.它似乎没有成功.请查看以下代码.它甚至没有编译
import java.util.*;
class Sample {
public List<String> getT(String s) {
List<String> list = new ArrayList<String>();
list.add(s);
return list;
}
public <T> void test(){
T list = getT("test");
System.out.println(l);
}
public static void main(String[] a) {
new Sample().test();
}
}
Run Code Online (Sandbox Code Playgroud)
再次修改了源并对其进行了测试,结果导致了编译时错误
public <T> List<T> getT(T s) {
List<T> list = new ArrayList<T>();
list.add(s);
return …Run Code Online (Sandbox Code Playgroud) class One {
public void doThing(One o) {System.out.println("One");}
}
class Two extends One{
public void doThing(Two t) {System.out.println("Two");}
}
public class Ugly {
public static void main(String[] args) {
Two t = new Two();
One o = t;
o.doThing(new Two());
}
}
Run Code Online (Sandbox Code Playgroud)
结果:一
class One {
public void doThing(One o) {System.out.println("One");}
}
class Two extends One{
public void doThing(Two t) {System.out.println("Two");}
}
public class Ugly {
public static void main(String[] args) {
Two t = new Two();
One o = …Run Code Online (Sandbox Code Playgroud) 以下代码编译并成功运行,没有任何异常
import java.util.ArrayList;
class SuperSample {}
class Sample extends SuperSample {
@SuppressWarnings("unchecked")
public static void main(String[] args) {
try {
ArrayList<Sample> sList = new ArrayList<Sample>();
Object o = sList;
ArrayList<SuperSample> ssList = (ArrayList<SuperSample>)o;
ssList.add(new SuperSample());
} catch (Exception e) {
e.printStackTrace();
}
}
}
Run Code Online (Sandbox Code Playgroud)
不应该ArrayList<SuperSample> ssList = (ArrayList<SuperSample>)o;生产线ClassCastException?
虽然下面的代码产生编译时错误错误以防止堆污染,但上面提到的代码不应该在运行时进行类似的预防吗?
ArrayList<Sample> sList = new ArrayList<Sample>();
ArrayList<SuperSample> ssList = (ArrayList<SuperSample>) sList;
Run Code Online (Sandbox Code Playgroud)
编辑:
如果Type Erasure是这背后的原因,是否应该有其他机制来防止无效对象被添加到List?例如
String[] iArray = new String[5];
Object[] iObject = iArray;
iObject[0]= …Run Code Online (Sandbox Code Playgroud) 具有public变量的类在Encapsulation中被认为是弱的并且它是不是一个糟糕的设计实践?
如果是这种情况下,为什么java.awt.Dimension有2个公共变量width和height?
书Javascript:权威指南在本章的第6版中陈述了以下内容
4.13.3删除运算符
var a = [1,2,3]; // Start with an array
delete a[2]; // Delete the last element of the array
a.length // => 2: array only has two elements now
Run Code Online (Sandbox Code Playgroud)
但是当我在Firefox和Chrome中尝试上述代码片段时,数组的长度仍为3.
这是本书中的错误信息,还是浏览器中javascript的实现与javascript规范不同?
注意:
我试过splice并删除了元素.我怀疑是ecmascript规范中是否有一个规范说删除应删除元素,浏览器决定不根据该脚本实现.或者这本书错了..?
String s = "test -||- testing again -|- test_1 -||- testing again_1";
StringTokenizer tokenizer = new StringTokenizer(s,"-|-");
System.out.println(tokenizer.countTokens());
while(tokenizer.hasMoreTokens()) {
System.out.println(tokenizer.nextToken());
}
Run Code Online (Sandbox Code Playgroud)
输出:
4
test
testing again
test_1
testing again_1
Run Code Online (Sandbox Code Playgroud)
算不应该是2 ..?
我尝试打印令牌,所有字符串都打印出来.不仅应将其视为一种标记.
我还从java API doc中读到了以下内容,
分隔符用于分隔令牌.令牌是不是分隔符的连续字符的最大序列
如果是这种情况,不应该使用我的分隔符" - | - "将字符串分成2个?
以下是我的代码
function callName() {
var name="x";
function printName(){
alert(name);
}
return printName;
}
name = callName();
alert(name);
name();
Run Code Online (Sandbox Code Playgroud)
当我执行它时,alert语句正在完美地打印printName函数,但函数调用name()给出一个错误,指出字符串不是函数.如果范围是问题,警报应该打印名称而不是函数.我试图理解这里的闭包,并试图解决这个问题并与js中的范围管理混淆.
这条线很好
Thread t = \u006E\u0065\u0077\u0020\u0054\u0068\u0072\u0065\u0061\u0064\u0028\u0029\u003B
Run Code Online (Sandbox Code Playgroud)
这是文本的unicode new Thread();
我的问题是接受" "或之外的unicode字符的需要是什么' '.我们可以在字符串文字和字符文字中使用unicodes.但实际代码本身需要接受它的必要性是什么?
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="media/js/jquery.js"></script>
<script>
var table = "";
var example_array = [];
function tablething() {
index = document.getElementById("location").value;
value = document.getElementById("input_value").value;
addRow(index,value);
}
function addRow(index,value){
var rowBeg = "<tr><td>";
var rowEnd = "</td></tr>";
value = rowBeg + value + rowEnd;
example_array.splice(index,0,value);
console.log(example_array);
table.innerHTML = example_array;
console.log(table.innerHTML);
}
$( document ).ready(function() {
table = document.getElementById("example");
example_array = new Array();
});
</script>
</head>
<body>
<table id="example">
</table>
Location: <input type="text" id="location"/>
Value: <input type="text" id="input_value"/>
<button onclick="tablething()">Add</button>
</body> …Run Code Online (Sandbox Code Playgroud)