Java 在数组列表的索引处添加值
我需要制作自己的数据结构,其中一部分是制作ArrayList。我需要确保我可以在元素n上添加一个对象,同时向下推所有其他对象。这是我的密码。现在它正在两次添加元素。它是函数public ReturnObject add(int index,Object item)。我需要这个函数在指定的索引处添加对象,然后向下移动其他对象Java 在数组列表的索引处添加值,java,arraylist,Java,Arraylist,我需要制作自己的数据结构,其中一部分是制作ArrayList。我需要确保我可以在元素n上添加一个对象,同时向下推所有其他对象。这是我的密码。现在它正在两次添加元素。它是函数public ReturnObject add(int index,Object item)。我需要这个函数在指定的索引处添加对象,然后向下移动其他对象 public class ArrayList implements List{ public static final int CAPACITY=16; pr
public class ArrayList implements List{
public static final int CAPACITY=16;
private int size = 0;
private Object[] data;
ReturnObjectImpl ro;
//constructors
public ArrayList() {
data = new Object[CAPACITY];
} //CONSTRUCTS LIST WITH DEFAULT CAPACITY
public ArrayList(int capacity) { // constructs list with given capacity
data = new Object[capacity];
System.out.println("Created an ArrayList of capacity " + capacity);
}
public boolean isEmpty(){
if(size == 0) {
// System.out.println("The list is empty");
return true;
}
return false;
}
public int size(){
System.out.println("The ArrayList is not full, but currently has " + size + " indexs");
return size;
}
public ReturnObject get(int index){
ro = new ReturnObjectImpl(data[index]);
return ro;
}
public ReturnObject remove(int index){
return null;
}
public ReturnObject add(int index, Object item){
if(index <= size && index < data.length){
for (int x = size-1; x >= index; x--){
data[x+1] = data[x];
data[index] = item;
ro = new ReturnObjectImpl(data[index]);
size++;
}
System.out.println("Added to array at " + index);
}
return ro;
}
public ReturnObject add(Object item){
if (data[0] == null){
data[0] = item;
}
//int adding = size + 1;
data[size] = item;
System.out.println("Added item to index " + size);
size++;
return null;
}
//added - but DELETE BEFORE SUBMITTING
public void printAll(){
for(int x = 0; x < data.length; x++){
System.out.println(data[x]);
}
}
}
公共类ArrayList实现列表{
公共静态最终int容量=16;
私有整数大小=0;
私有对象[]数据;
反渗透;
//建设者
公共数组列表(){
数据=新对象[容量];
}//使用默认容量构造列表
公共ArrayList(int-capacity){//构造具有给定容量的列表
数据=新对象[容量];
System.out.println(“创建了容量的数组列表”+容量);
}
公共布尔值为空(){
如果(大小==0){
//System.out.println(“列表为空”);
返回true;
}
返回false;
}
公共整数大小(){
System.out.println(“ArrayList未满,但当前有“+size+”索引”);
返回大小;
}
public ReturnObject get(int索引){
ro=新的ReturnObjectImpl(数据[索引]);
返回ro;
}
公共返回对象删除(int索引){
返回null;
}
public ReturnObject add(int索引,对象项){
if(index=index;x--){
数据[x+1]=数据[x];
数据[索引]=项目;
ro=新的ReturnObjectImpl(数据[索引]);
大小++;
}
System.out.println(“添加到“+索引处的数组中”);
}
返回ro;
}
公共返回对象添加(对象项){
如果(数据[0]==null){
数据[0]=项目;
}
//整数相加=大小+1;
数据[大小]=项目;
System.out.println(“添加到索引的项目”+大小);
大小++;
返回null;
}
//添加-但在提交前删除
public void printAll(){
对于(int x=0;x
除了“s”,您可以使用System.arrayCopy()
将右侧部分“移动”到右侧,而不是循环的。您只需要知道内部数组(数据
)是否已满。如果是,则必须展开内部阵列
System.arraycopy(this.data, insertIndex, this.data, insertIndex + 1, 1);
一些注意事项:
- 代码
if (data[0] == null) {
data[0] = item;
}
if (size == 0) {
// System.out.println("The list is empty");
return true;
}
return false;
如果调用了ArrayList(0)
,将抛出ArrayIndexOutOfBoundsException
- 代码
if (data[0] == null) {
data[0] = item;
}
if (size == 0) {
// System.out.println("The list is empty");
return true;
}
return false;
可以重写为
return (size == 0);
- 您似乎忽略了更多的检查,比如检查内部数组是否已满。当前代码不会扩展内部数组,因此如果添加的对象超过初始容量(默认值16),则会抛出
ArrayIndexOutOfBoundsException
除了“s”,您可以使用System.arrayCopy()
将右侧部分“移动”到右侧,而不是循环的。您只需要知道内部数组(数据
)是否已满。如果是,则必须展开内部阵列
System.arraycopy(this.data, insertIndex, this.data, insertIndex + 1, 1);
一些注意事项:
- 代码
if (data[0] == null) {
data[0] = item;
}
if (size == 0) {
// System.out.println("The list is empty");
return true;
}
return false;
如果调用了ArrayList(0)
,将抛出ArrayIndexOutOfBoundsException
- 代码
if (data[0] == null) {
data[0] = item;
}
if (size == 0) {
// System.out.println("The list is empty");
return true;
}
return false;
可以重写为
return (size == 0);
- 您似乎忽略了更多的检查,比如检查内部数组是否已满。当前代码不会扩展内部数组,因此如果添加的对象超过初始容量(默认值16),则会抛出
ArrayIndexOutOfBoundsException
显然,将对象插入该数组时:
for (int x = size-1; x >= index; x--){
data[x+1] = data[x];
data[index] = item;
这不应该发生在循环中!插入应该在正确的索引处恰好发生一次!因此,即使您保持该循环以移动其他元素,最后一个赋值也应在该“移动循环”结束后移动
关键是:您应该后退一步,仔细查看这个循环使用它的循环变量做了什么
换句话说:要么拿一张纸自己“运行”代码;或者在调试器中运行它
因为这可能是一种家庭作业活动,我就不说了;它应该足以让您继续并帮助您修复代码。显然,在将对象插入该数组时:
for (int x = size-1; x >= index; x--){
data[x+1] = data[x];
data[index] = item;
这不应该发生在循环中!插入应该在正确的索引处恰好发生一次!因此,即使您保持该循环以移动其他元素,最后一个赋值也应在该“移动循环”结束后移动
关键是:您应该后退一步,仔细查看这个循环使用它的循环变量做了什么
换句话说:要么拿一张纸自己“运行”代码;或者在调试器中运行它
因为这可能是一种家庭作业活动,我就不说了;这应该足以让您继续并帮助您修复代码。“我需要创建自己的数据结构”为什么?您知道提供的数组列表的源可能是一个赋值。您有一个循环,其目的是移位元素。循环体将对需要移位的每个元素执行一次。您不希望每次移动一个新元素时都创建一个新元素,是吗?因此,添加新元素的代码需要从循环中移出。难道你不能查看java.util.ArrayList
的源代码并将其删除吗?我需要将其作为一项任务来完成。“我需要创建自己的数据结构”为什么?您知道提供的数组列表的源是?可能是一个赋值。您有一个循环,其目的是移位元素