之间有什么区别List,List<?>,List<T>,List<E>,和List<Object>?
现在我不要盲目地问这个问题,所以请不要关闭这个帖子.我先介绍一下基本代码:
public static void test(List<?> list){
System.out.println(list); // Works
}
Run Code Online (Sandbox Code Playgroud)
我明白:
1 List.:是原始类型,因此不是typesafe.它只会在强制转换时生成运行时错误.当演员表不好时我们想要编译时错误.不建议使用.
2 . List<?>:是一个无界的通配符.但不确定这是为了什么?所以如果我改变List<?>方法
public static void test(List<?> list){
list.add(new Long(2)); // Error
list.add("2"); // Error
System.out.println(list);
}
Run Code Online (Sandbox Code Playgroud)
它仍然很好.如果您能解释一下这种用法,我将不胜感激.
编辑:如果我这样做:
public static void test(List<T> list){ // T cannot be resolved
System.out.println(list);
}
Run Code Online (Sandbox Code Playgroud)
但如果我改成List<?>这个:
public <T> T[] toArray(T[] a){
return a;
}
Run Code Online (Sandbox Code Playgroud)
3 . <T>:
public static …Run Code Online (Sandbox Code Playgroud) 我想知道是否可以编写一个接受多种泛型类型的函数,如下所示:
public int void myfunction(Set<T> a, Set<T> b) {
return 5;
}
Set<Integer> setA = new HashSet<Integer>();
Set<String> setB = new HashSet<String>();
int result = myfunction(setA, setB);
Run Code Online (Sandbox Code Playgroud)
那会有用吗?每个参数中的泛型是否意味着每个参数必须具有相同的通用类型T?
谢谢!
我真的不明白泛型的意义.他们做了什么,你如何使用它们?
据我所知,他们所做的只是在编译时检查返回类型而不是运行时间,以避免在抛出错误之前运行程序.这就是他们所做的一切吗?
例如:
public <Integer> int test() {
return 'c'; //will throw error at compile instead of runtime
}
Run Code Online (Sandbox Code Playgroud)
我正在读一些关于泛型是如何随意的,你应该只使用大写字母?这有点令人困惑.
为什么我们在使用时会丢失类型安全性List而不是在使用时List<Object>?他们基本上不一样吗?
编辑:我发现以下给出了编译错误
public class TestClass
{
static void func(List<Object> o, Object s){
o.add(s);
}
public static void main(String[] args){
func(new ArrayList<String>(), new Integer(1));
}
}
Run Code Online (Sandbox Code Playgroud)
而事实并非如此
public class TestClass
{
static void func(List o, Object s){
o.add(s);
}
public static void main(String[] args){
func(new ArrayList<String>(), new Integer(1));
}
}
Run Code Online (Sandbox Code Playgroud)
为什么?
我一直试图简单地为声明的<>部分命名,但没有运气.任何人都可以告诉我它叫什么,我怎么可以在我自己的课堂上使用它?例如,我可能想尝试制作我自己的集合,并使用new MyThing<String>例如.任何帮助表示赞赏,谢谢!
可能重复:
Java泛型
在Eclipse中,我收到了关于使用'rawtypes'的警告,其中一个修复就是添加<?>.例如:
Class parameter = String.class;
//Eclipse would suggest a fix by converting to the following:
Class<?> parameter = String.class;
Run Code Online (Sandbox Code Playgroud)
这<?>究竟意味着什么?
多年来我看到很多人都使用"泛型"这个词,老实说我不知道它意味着什么,不管它是什么我最有可能使用它但只是不知道它被称为那个.:p
可能重复:
Java泛型
更具体地说,<String>在以下代码行中的作用是什么?
private List<String> item = new ArrayList<String>();
Run Code Online (Sandbox Code Playgroud) 随机码:
// Getting All Contacts
public List<Contact> getAllContacts() {
List<Contact> contactList = new ArrayList<Contact>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_CONTACTS;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Contact contact = new Contact();
contact.setID(Integer.parseInt(cursor.getString(0)));
contact.setName(cursor.getString(1));
contact.setPhoneNumber(cursor.getString(2));
// Adding contact to list
contactList.add(contact);
} while (cursor.moveToNext());
}
// return contact list
return contactList;
}
Run Code Online (Sandbox Code Playgroud)
看看它在哪里
public List<Contact> getAllContacts() { …Run Code Online (Sandbox Code Playgroud) 我已经创建了一个方法如下
static <N> N addTwoString(N a, N b){
StringBuilder sb = new StringBuilder();
sb.append(a);
sb.append(b);
return sb.toString();
}
public static void main(String[] args)
{
addTwoString("a", "b");
}
Run Code Online (Sandbox Code Playgroud)
对于这种情况,我传递两个字符串并追加它并使用StringBuilder返回它.无论如何,我正在错误的回报声明说类型不匹配:不能转换从字符串到N.我的问题是这个方法接受字符串值(甚至类型是N)没有任何问题,但为什么它在return语句中给出错误?
java ×12
generics ×8
collections ×1
hashmap ×1
instance ×1
oop ×1
parameters ×1
raw-types ×1
syntax ×1
tags ×1