我正在学习编码,目前正在使用对象数组进行编程。
我现在的问题: 我想在按下按钮时在数组中的最后一个存储对象之后添加对象。 cdarray.length-1设置数字字段中的值,它将我的arrayid保存为99.这不是我想要的。它应该从0开始,因为那样,我想开始添加对象。
任何人都可以帮助我吗? :P
会很好
CD[] cdarray = new CD[100];
nf_objektID.setInt(cdarray.length-1);
cdarray[cdarray.length-1] = new CD();
答案 0 :(得分:1)
我喜欢初学者想要学习实现而不是使用现有的库。有些人会说你正在重新发明轮子,但忽略了这一点。你将学习基础知识并掌握科学知识。要回答你的问题,请查看ArrayList.add()
的源代码 329: /**
330: * Appends the supplied element to the end of this list.
331: * The element, e, can be an object of any type or null.
332: *
333: * @param e the element to be appended to this list
334: * @return true, the add will always succeed
335: */
336: public boolean add(E e)
337: {
338: modCount++;
339: if (size == data.length)
340: ensureCapacity(size + 1);
341: data[size++] = e;
342: return true;
343: }