我正在尝试实现自己的ArrayList而不使用java集合进行练习。在这个阶段我想实现两个主要方法,add(E)和get(int)tp得到这个想法。我的代码如下。但是我遇到了一些问题:
PS。请不要回答问题3,并将我的蔗糖代码转给我一两个!
import java.util.Arrays;
public class MyArrayList<E>{
private final int DEFAULT_SIZE=2;
private Object[] myData = new Object[DEFAULT_SIZE];
private int actSize=0;
public boolean add(E data){
if (actSize>=myData.length/2){
increaseSize();
}
myData[actSize++] = data;
return true;//when can it be false?
}
private void increaseSize()throws RuntimeException{
myData = Arrays.copyOf(myData, myData.length*2);
}
public E get(int index) throws RuntimeException{
if (index >= actSize){
throw new IndexOutOfBoundsException();
}
return (E) myData[index];
}
public static void main(String[] args) {
MyArrayList<String> arList = new MyArrayList<>();
arList.add("Hello");
arList.add("Bye bye!");
System.out.println(arList.get(1));// prints Bye bye! which is correct
}
}
答案 0 :(得分:3)
“return(E)myData [index]”行发出警告“类型安全: 未选中从对象转换为E“。如何解决
取消警告
@SuppressWarnings("unchecked")
ArrayList.add(T)
的Java 7实现返回boolean
。 在什么情况下add()
必须返回false
。在什么 逻辑它返回false
并何时返回true
?
请参阅javadoc
<强>返回强>:
true
(由Collection.add(E)
指定)
它始终返回true
。
在哪里可以找到ArrayList的java 7实现的源代码
在JDK安装的src.zip
存档中,或通过搜索
java ArrayList源代码
答案 1 :(得分:2)
“return(E)myData [index]”行发出警告“类型安全:未选中从对象转换为E”。我该如何解决这个问题?
由于您正在使用通用数组,因此您总是会有此未经检查的强制转换警告。泛型和数组don't really mix一切都很好,但更好的约定是将通用类型附加到数组中:
private E[] myData = (E[]) new Object[DEFAULT_SIZE];
您可以随时将@SuppressWarnings("unchecked")
添加到字段本身,以使该警告消失。
@SuppressWarnings("unchecked")
private E[] myData = (E[]) new Object[DEFAULT_SIZE];
ArrayList.add(T)的Java 7实现,返回一个布尔值。在什么情况下add()必须返回false。在什么逻辑下它返回false并返回true?
这是一个有趣的问题。通常情况下,可以预期add
的限制来自Collections#add
:
支持此操作的集合可能会限制可能添加到此集合的元素。特别是,某些集合将拒绝添加null元素,而其他集合将对可能添加的元素类型施加限制。集合类应在其文档中明确指出可以添加哪些元素的任何限制。
...但是,因为ArrayList
的特殊之处在于它的设计目的是在它即将用完时扩展它的空间,它(理论上)总是能够添加因此,它应总是返回true
。
在哪里可以找到ArrayList的Java 7实现的源代码?
Grepcode通常是一个很好的资源。如果你下载了带源代码的JDK,你也可以在src.zip中找到它。
答案 2 :(得分:0)
@SupressWarnings("unchecked")
。答案 3 :(得分:-1)
我在评论中做了很少的解释,因为很清楚要理解。
public class MyArrayList<E extends Object> {
private static int initialCapacity = 5;
private static int currentSize;
private Object[] myArrayList = {}, temp = {};
private static int currentIndex = 0;
public static void main(String[] args) {
MyArrayList arrList = new MyArrayList();
arrList.add("123"); //add String
arrList.printAllElements();
arrList.add(new Integer(111)); //add Integer
arrList.printAllElements();
arrList.add(new Float("34.56")); //add Integer
arrList.printAllElements();
arrList.delete("123");
arrList.printAllElements();
arrList.delete(123);
arrList.printAllElements();
arrList.delete(123);
arrList.printAllElements();
}
public MyArrayList() { //creates default sized Array of Objects
myArrayList = new Object[initialCapacity]; //generic expression
/* everytime I cross my capacity,
I make double size of Object Array, copy all the elements from past myObject Array Object
*/
}
public MyArrayList(int size) { //creates custom sized Array of Objects
myArrayList = new Object[size];
}
public void add(Object anyObj) {
//add element directy
myArrayList[currentIndex] = anyObj;
currentSize = myArrayList.length;
currentIndex++;
if (currentIndex == currentSize) {
createDoubleSizedObjectArray(currentSize);
}
}
//print all elements
public void printAllElements() {
System.out.println("Displaying list : ");
for (int i = 0; i < currentIndex; i++) {
System.out.println(myArrayList[i].toString());
}
}
private void createDoubleSizedObjectArray(int currentSize) {
temp = myArrayList.clone();
myArrayList = new MyArrayList[2 * currentSize]; //myObject pointer big size data structure
// myObject = temp.clone(); //probably I can do this here as well. Need to check this
System.arraycopy(temp, 0, myArrayList, 0, currentSize);
}
void delete(Object object) {
//if already empty
if (currentIndex == 0) {
System.out.println("Already empty!");
return;
}
//you don't need to delete anything. I can simply override the storage
currentIndex--;
}
}