英文:
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("Enter the element you want to edit :");
element=sc.nextInt();
for(int J=arr1.length-1 ; J>element; J--) {
arr1[J] = arr1[J-1];
System.out.print("Enter the you number want to update: ");
toUpdate=sc.nextInt();
arr1[element]=toUpdate;
System.out.println("Update successful...");
}
答案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("Enter the element you want to edit : ");
int element = sc.nextInt();
System.out.print("Enter the you number want to update: ");
int toUpdate = sc.nextInt();
// Iterate the array using a for loop.
for(int i = 0; i < 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 -> i == element ? toUpdate : i).toArray();
Or even Arrays.setAll()
method:
Arrays.setAll(array, i -> 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 < 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 < arr.length; i++)
if (arr[i] == prvVal)
arr[i] = newVal;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论