相关文章
更多最近更新
更多关于ArrayList的使用问题~~~~~~~
2019-03-25 13:47|来源: 网路
各位高手大家好,本人刚学Java,对很多东西都不是很清楚,这几天我照着书上敲了好几个程序,放到Eclipse里去编译(注:我的Eclipse是3.2中文版的),老是显示出错,程序如下(出问题的语句用红色标出):
/*!Begin Snippet:file*/
import java.util.*;
import java.text.*;
/**
* Maintains a collection of {@link Borrower} objects.
*
* @author XXX
* @version 1.0.0
* @see Borrower
*/
public class BorrowerDatabase implements Iterable<Borrower> {
//问题是:放到第一个上时显示,类型Iterable不是通用的;不能使用参数<Borrower>将其参数化!!!
//放到第二个,即<Borrower>上时显示,语法错误,仅当源级别为5.0时已参数化的类型方可用。
/* Collection of <code>Borrower</code> objects.*/
private ArrayList<Borrower> borrowers;
//问题是:放到第一个上时显示,类型ArrayList不是通用的;不能使用参数<Borrower>将其参数化!!!
//放到第二个,即<Borrower>上时显示,语法错误,仅当源级别为5.0时已参数化的类型方可用。
/**
* Constructs an empty collection of {@link Borrower}
* objects.
*/
public BorrowerDatabase() {
this.borrowers = new ArrayList<Borrower>();
}
/**
* Adds a {@link Borrower} object to this collection.
*
* @param borrower the {@link Borrower} object.
*/
public void addBorrower(Borrower borrower) {
this.borrowers.add(borrower);
}
/**
* Returns an iterator over the borrowers in this database.
*
* return an {@link Iterator} of {@link Borrower}
*/
public Iterator<Borrower> iterator() {
//问题同上~~~~~~~~~~~
return this.borrowers.iterator();
}
/**
* Returns the {@link Borrower} object with the specified
* <code>id</code>.
*
* @param id the id of the borrower.
* @return The {@link Borrower} object with the specified id.
* Returns <code>null</code> if the object with the
* id is not found.
*/
public Borrower getBorrower(String id) {
for (Borrower borrower : this.borrowers) {
if (borrower.getId().equals(id)) {
return borrower;
}
}
return null;
}
/**
* Returns the number of {@link Borrower} objects in this collection.
*
* @return the number of {@link Borrower} objects in this collection.
*/
public int getNumberOfBorrowers() {
return this.borrowers.size();
}
}
/*!End Snippet:file*/
注:类Borrower已存在并成功编译。
这是为什么啊,我已经在网上查了很久,也问了很多人,但没人知道,郁闷至极~~~~~~~~~
望各位能够不吝赐教,小生将感激不尽啊!!!
问题补充:
1.Borrower类已存在啊,并且已引入到该包下面,但还是不行,Eclipse一直报错,哎~~~~~
2.依据lixc的答复我也试了,但没发现Compiler comliance level啊!真是惭愧,能否麻烦你讲具体点在哪?谢谢了~~~~~~~~~
/*!Begin Snippet:file*/
import java.util.*;
import java.text.*;
/**
* Maintains a collection of {@link Borrower} objects.
*
* @author XXX
* @version 1.0.0
* @see Borrower
*/
public class BorrowerDatabase implements Iterable<Borrower> {
//问题是:放到第一个上时显示,类型Iterable不是通用的;不能使用参数<Borrower>将其参数化!!!
//放到第二个,即<Borrower>上时显示,语法错误,仅当源级别为5.0时已参数化的类型方可用。
/* Collection of <code>Borrower</code> objects.*/
private ArrayList<Borrower> borrowers;
//问题是:放到第一个上时显示,类型ArrayList不是通用的;不能使用参数<Borrower>将其参数化!!!
//放到第二个,即<Borrower>上时显示,语法错误,仅当源级别为5.0时已参数化的类型方可用。
/**
* Constructs an empty collection of {@link Borrower}
* objects.
*/
public BorrowerDatabase() {
this.borrowers = new ArrayList<Borrower>();
}
/**
* Adds a {@link Borrower} object to this collection.
*
* @param borrower the {@link Borrower} object.
*/
public void addBorrower(Borrower borrower) {
this.borrowers.add(borrower);
}
/**
* Returns an iterator over the borrowers in this database.
*
* return an {@link Iterator} of {@link Borrower}
*/
public Iterator<Borrower> iterator() {
//问题同上~~~~~~~~~~~
return this.borrowers.iterator();
}
/**
* Returns the {@link Borrower} object with the specified
* <code>id</code>.
*
* @param id the id of the borrower.
* @return The {@link Borrower} object with the specified id.
* Returns <code>null</code> if the object with the
* id is not found.
*/
public Borrower getBorrower(String id) {
for (Borrower borrower : this.borrowers) {
if (borrower.getId().equals(id)) {
return borrower;
}
}
return null;
}
/**
* Returns the number of {@link Borrower} objects in this collection.
*
* @return the number of {@link Borrower} objects in this collection.
*/
public int getNumberOfBorrowers() {
return this.borrowers.size();
}
}
/*!End Snippet:file*/
注:类Borrower已存在并成功编译。
这是为什么啊,我已经在网上查了很久,也问了很多人,但没人知道,郁闷至极~~~~~~~~~
望各位能够不吝赐教,小生将感激不尽啊!!!
问题补充:
1.Borrower类已存在啊,并且已引入到该包下面,但还是不行,Eclipse一直报错,哎~~~~~
2.依据lixc的答复我也试了,但没发现Compiler comliance level啊!真是惭愧,能否麻烦你讲具体点在哪?谢谢了~~~~~~~~~
相关问答
更多-
ArrayList与List[2022-07-17]
LIST是一个接口 ARRAYLIST是一个实现类,实现了4个接口,其中包含了list。所以子类能把实例传给父类,父类能够使用他自己定义了的方法,但是不能使用ArrayList 自己特定一些方法,或者是实现从别人的接口的方法。 ArrayList list = new ArrayList() 就是普通类实例化。。 -
以下是List接口(ArrayList实现)的一部分: E e = list.get(list.size() - 1); E是元素类型。 如果列表为空,则抛出IndexOutOfBoundsException 。 您可以在这里找到整个API文档。 The following is part of the List interface (which ArrayList implements): E e = list.get(list.size() - 1); E is the element type. ...
-
您可以在特定索引处选择ArrayList,并将其传递给构造函数,如下所示: int index = 2; // the index of the list you want to pass to the constructor MyNewObject newObject = new MyNewObject(bigList.get(index)); You can select an ArrayList at a particular index and pass it to a constructor a ...
-
Arraylist使用数组?(Arraylist uses Array?)[2022-11-15]
是的,它确实。 验证这一点的最简单方法之一是查看源代码。 您可以将手放在参考源上 ,也可以简单地使用.NET Reflector来反编译.NET DLL。 以下是来自Reflector的ArrayList的相关部分: public class ArrayList : IList, ICollection, IEnumerable, ICloneable { static ArrayList() { emptyArray = new object[0]; } ... -
Arraylits的ArrayList(ArrayList of Arraylits)[2023-01-23]
要将数字添加到列表列表中的列表,您必须使用其索引get它,然后您可以add它。 lists.get(0).add(42); To add a number to a list in the list of lists, you have to get it using its index, then you can add to it. lists.get(0).add(42); -
arraylist中的Arraylist(Arraylist in arraylist)[2023-10-17]
ListinnerList = new ArrayList (); // add Bar objects to this list List - > mainList = new ArrayList
- >();
mainList.add(innerList); // adding innerList to mainList
for(List
listElement:mainList){ // here listElement will g ... -
你从来没有真正初始化那些数组列表。 你要 ArrayList
> mainArray = new ArrayList >(); // or new ArrayList<>() in java 7 并在内部循环 : ArrayList innerArrays = new ArrayList (); // or new ArrayList<>() in java 7 此外,千万不要压制警告“只是为了使代码工作” ... -
使用ArrayList
>时出现MalformedParameterizedTypeException(MalformedParameterizedTypeException when use ArrayList [2021-09-03]>) ArrayList> 没有任何意义(我认为这是错误告诉你的) 它应该只是: ArrayList > 要说你有一个整数列表列表......泛型不表示列表中有多少个元素 ArrayList > doesn't make any sense (which I think is what the error is telling ... -
您需要将arr2.get(0)强制转换为ArrayList,以便可以在其上调用get方法 (您的ArrayList ArrayList arr = new ArrayList();未键入 - 因此它包含Object的实例)。 喜欢这个: package test; import java.util.ArrayList; public class ArrayListPractice { public static void main(String[] args){ ArrayList ...
-
public static ArrayList
Array = new ArrayList (); public static ArrayList resim_list=new ArrayList (); 在你的活动中 Array = bundle.getStringArrayList("string-array"); // already Array is initalized. no need to initializ ...