从数组中移除一个元素,而不使用列表。

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

Removing an element from an array without using list

问题

我一直在努力编写一个方法,通过创建一个新数组,将其大小减小1,并将除了“position”位置上的项目之外的所有内容复制过去,以便从数组中删除项目。有什么原因导致我的代码不起作用吗?不过我不能使用列表函数,必须使用数组。

  1. int newArray[] = new int[originalArray.length - 1];
  2. for (int i = 0; i < originalArray.length; i++) {
  3. if (i < position)
  4. newArray[i] = originalArray[i];
  5. else if (i > position)
  6. newArray[i - 1] = originalArray[i];
  7. }
英文:

I've been trying for ages to make a method that lets me remove an item from an array by making a new array, reducing it's size by 1, and then copying everything over except for the item in "position". Any reason why my code wouldn't be working? I can't use the list function though, I have to use arrays.

  1. int newArray[] = new int[originalArray.length - 1];
  2. for (int i = 0; i &lt; originalArray.length; i++) {
  3. if (i &lt; position)
  4. newArray[i] = originalArray[i];
  5. else if (i == position)
  6. i++;
  7. else if (i &gt; position)
  8. newArray[i] = originalArray[i];
  9. }

答案1

得分: 1

你不应该尝试编写超出范围的元素,尝试这样做:

  1. int newArray[] = new int[originalArray.length - 1];
  2. for (int i = 0; i < newArray.length; i++) {
  3. if (i < position)
  4. newArray[i] = originalArray[i];
  5. else
  6. newArray[i] = originalArray[i+1];
  7. }
英文:

You shouldn't try to write element out of range, try this:

  1. int newArray[] = new int[originalArray.length - 1];
  2. for (int i = 0; i &lt; newArray.length; i++) {
  3. if (i &lt; position)
  4. newArray[i] = originalArray[i];
  5. else
  6. newArray[i] = originalArray[i+1];
  7. }

答案2

得分: 1

  1. public static int[] removeElement(int[] originalArray, int position) {
  2. int newArray[] = new int[originalArray.length - 1];
  3. for (int i = 0; i < originalArray.length; i++) {
  4. if (i < position)
  5. newArray[i] = originalArray[i];
  6. else if (i > position)
  7. newArray[i - 1] = originalArray[i];
  8. }
  9. return newArray;
  10. }
  11. // P.S. I reccomend you to use `System.arraycopy()`:
  12. public static int[] removeElement(int[] arr, int pos) {
  13. if (pos < 0 || pos >= arr.length)
  14. return arr;
  15. if (arr.length == 1)
  16. return new int[0];
  17. int[] res = new int[arr.length - 1];
  18. System.arraycopy(arr, 0, res, 0, pos);
  19. System.arraycopy(arr, pos + 1, res, pos, arr.length - pos - 1);
  20. return res;
  21. }
  22. // Output:
  23. int[] arr = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  24. int[] res = removeElement(arr, 4);
  25. System.out.println(Arrays.toString(res)); // [0, 1, 2, 3, 5, 6, 7, 8, 9]
英文:
  1. public static int[] removeElement(int[] originalArray, int position) {
  2. int newArray[] = new int[originalArray.length - 1];
  3. for (int i = 0; i &lt; originalArray.length; i++) {
  4. if (i &lt; position)
  5. newArray[i] = originalArray[i];
  6. else if (i &gt; position)
  7. newArray[i - 1] = originalArray[i];
  8. }
  9. return newArray;
  10. }

P.S. I reccomend you to use System.arraycopy():

  1. public static int[] removeElement(int[] arr, int pos) {
  2. if (pos &lt; 0 || pos &gt;= arr.length)
  3. return arr;
  4. if (arr.length == 1)
  5. return new int[0];
  6. int[] res = new int[arr.length - 1];
  7. System.arraycopy(arr, 0, res, 0, pos);
  8. System.arraycopy(arr, pos + 1, res, pos, arr.length - pos - 1);
  9. return res;
  10. }

Output:

  1. int[] arr = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  2. int[] res = removeElement(arr, 4);
  3. System.out.println(Arrays.toString(res)); // [0, 1, 2, 3, 5, 6, 7, 8, 9]

huangapple
  • 本文由 发表于 2020年10月24日 06:31:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/64508056.html
匿名

发表评论

匿名网友

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

确定