通用列表添加方法

huangapple go评论89阅读模式
英文:

generic list add method

问题

我尝试创建一个方法,在此列表的指定位置插入元素。然后通过将该位置处的元素和随后的元素右移一个位置,使它们的索引加一。我知道有一个这个方法的快捷方式,但我应该自己完成它,这是我尝试做的,但它没有生效。

  1. private T a[];
  2. private int count;
  3. private int size = 0;
  4. public int size() { return size; }
  5. public void add(int index, T t) throws Exception {
  6. if (index < 0 || index >= a.length) {
  7. throw new IndexOutOfBoundsException();
  8. }
  9. Object[] temp = new Object[a.length + 1];
  10. for (int k = 0, j = 0; j < temp.length; ++k, ++j) {
  11. if (k == index) {
  12. temp[index] = t;
  13. --k;
  14. } else {
  15. temp[j] = a[index]; //
  16. }
  17. }
  18. a = (T[]) temp;
  19. }
英文:

I tried to make method which inserts element at the specified position in this list.
Then Shifts the element & subsequent elements currently at that position to the
Right by adding one to their indices, i know there is shortcut for this method but I am suppose to do it, here what i tried to do but it's not working.

  1. private T a[];
  2. private int count;
  3. private int size = 0;
  4. public int size() { return size; }
  5. public void add(int index,T t) throws Exception {
  6. if (index &lt; 0 || index &gt; = a.length){
  7. throw new IndexOutOfBoundsException();
  8. }
  9. Object[] temp = new Object[a.length + 1];
  10. for (int k = 0, j = 0; j &lt; temp.length; ++ k, ++ j){
  11. if ( k == index ) {
  12. temp[index] = t;
  13. --k;
  14. } else {
  15. temp[j] = a[index]; //
  16. }
  17. }
  18. a = (T[]) temp;
  19. }

答案1

得分: 1

移动元素的诀窍是从右边开始,所以:

  1. for (int i = size; i > index; i--) {
  2. a[i] = a[i - 1];
  3. }

顺便说一下,当增加大小时,通常应该将其大小翻倍,而不仅仅增加1。

英文:

The trick to shifting is to start from the right, so:

  1. for (int i = size; i &gt; index; i--) {
  2. a[i] = a[i - 1];
  3. }

btw, when increasing size, normallyyou would double its size,rather than just growing by 1.

答案2

得分: 0

我更正了你的 'for' 代码块,试试这个:

  1. for (int k = 0, j = 0; j < temp.length; ++k, ++j) {
  2. if (k == index) {
  3. temp[index] = t;
  4. --k;
  5. index = -1;
  6. } else {
  7. temp[j] = a[k];
  8. }
  9. }

我添加了2处修正:

  • index = -1; - 为了只在满足条件时进入if语句一次,否则它会不断进入条件内部。
  • temp[j] = a[k]; - 替换为 a[k],你之前总是从 a[index] 取值,也就是同一个位置,这是错误的。

祝你好运 通用列表添加方法

英文:

I corrected your 'for' block, try this:

  1. for (int k = 0, j = 0; j &lt; temp.length; ++k, ++j){
  2. if ( k == index ) {
  3. temp[index] = t;
  4. --k;
  5. index = -1;
  6. } else {
  7. temp[j] = a[k];
  8. }
  9. }

2 fixes i added:

  • index = -1; - In order to enter the if condition only 1 time, else it will constantly enter the condition
  • temp[j] = a[k]; - replaced to a[k], you was always taking value from a[index] means the same place, this is incorrect.

good luck 通用列表添加方法

huangapple
  • 本文由 发表于 2020年9月15日 14:23:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/63896238.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定