编辑或更新数组时应使用元素而不是索引。

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

Edit or update array with the element and not index

问题

int[] arr1 = {12, 4, 5, 2, 5};
int element;
int toUpdate;

System.out.print("Enter the element you want to edit: ");
element = sc.nextInt();

System.out.print("Enter the number you want to update to: ");
toUpdate = sc.nextInt();

for (int J = 0; J < arr1.length; J++) {
    if (arr1[J] == element) {
        arr1[J] = toUpdate;
        System.out.println("Update successful...");
        break; // If you want to update only the first occurrence, you can break the loop here.
    }
}
英文:

How do you edit an integer in an array by using the element and not the index of the array?
I have this code it asks for the index that I want to edit or change.

Lets say I have this array int[] array = {12, 4, 5, 2, 5};
I want to edit number 12 into 3, so I input in element is 12 and toUpdate 3, How to?

int items;
int element;
System.out.print(&quot;Enter the element you want to edit :&quot;);
element=sc.nextInt();

for(int J=arr1.length-1 ; J&gt;element; J--) {
      arr1[J] = arr1[J-1];                         
      System.out.print(&quot;Enter the you number want to update: &quot;);
      toUpdate=sc.nextInt();
      arr1[element]=toUpdate;
      System.out.println(&quot;Update successful...&quot;); 
   }

答案1

得分: 2

你可以使用for循环或者新的Java 8的stream库来迭代数组,并根据用户输入更改值。

import java.util.*;

public class Main {
    public static void main(String[] args) {
        int[] array = {12, 4, 5, 2, 5};

        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the element you want to edit: ");
        int element = sc.nextInt();
        System.out.print("Enter the number you want to update: ");
        int toUpdate = sc.nextInt();

        // 使用for循环迭代数组。
        for (int i = 0; i < array.length; i++) {
            array[i] = (array[i] == element) ? toUpdate : array[i];
        }

        System.out.println(Arrays.toString(array));
    }
}

或者你可以使用stream替代传统的for循环:map调用将所有出现的element更改为toUpdate

array = Arrays.stream(array).map(i -> i == element ? toUpdate : i).toArray();

或者甚至使用Arrays.setAll()方法:

Arrays.setAll(array, i -> array[i] == element ? toUpdate : array[i]);

输出:

Enter the element you want to edit: 12
Enter the number you want to update: 3
[3, 4, 5, 2, 5]
英文:

You could use either a for loop, or the new Java 8 stream library to iterate the array and change the value according to the user input.

import java.util.*;

public class Main {
    public static void main(String[] args) {
        int[] array = {12, 4, 5, 2, 5};

        Scanner sc = new Scanner(System.in);
        System.out.print(&quot;Enter the element you want to edit : &quot;);
        int element = sc.nextInt();
        System.out.print(&quot;Enter the you number want to update: &quot;);
        int toUpdate = sc.nextInt();

        // Iterate the array using a for loop.
        for(int i = 0; i &lt; array.length; i++) {
            array[i] = (array[i] == element) ? toUpdate : array[i];
        }
        
        System.out.println(Arrays.toString(array));
    }
}

Alternatively you could use a stream instead of the traditional for-loop:
The map call changes all occurrences of element to toUpdate.

array = Arrays.stream(array).map(i -&gt; i == element ? toUpdate : i).toArray();

Or even Arrays.setAll() method:

Arrays.setAll(array, i -&gt; array[i] == element ? toUpdate : array[i]);

Output:

Enter the element you want to edit : 12
Enter the you number want to update: 3
[3, 1, 2, 3, 4]

答案2

得分: 1

你可以再次遍历数组,检查待更新的元素是否存在,如果存在,则进行更新。

for (int i = 0; i < arr.length; i++){
    if (arr[i] == toUpdate){
        arr[i] = newUpdateValue;
        break;
    }
}

附注:这将仅更新它遇到的第一个值。

英文:

You can iterate through the array again and check if the element to be updated is present, if it is update it.

for (int i = 0; i &lt; arr.length; i++){
    if (arr[i] == toUpdate){
        arr[i] = newUpdateValue;
        break;
    }
}

P.S: This will update only the first value it comes across.

答案3

得分: 0

为了更新它,您应该迭代遍历arr中的所有值,并用newVal替换prvVal

public static void update(int[] arr, int prvVal, int newVal) {
    for (int i = 0; i < arr.length; i++)
        if (arr[i] == prvVal)
            arr[i] = newVal;
}
英文:

To update it, you should iterate over all values in arr and replase prvVal with newVal:

public static void update(int[] arr, int prvVal, int newVal) {
    for (int i = 0; i &lt; arr.length; i++)
        if (arr[i] == prvVal)
            arr[i] = newVal;
}

huangapple
  • 本文由 发表于 2020年10月11日 18:01:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/64302770.html
匿名

发表评论

匿名网友

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

确定