我在这里声明数据
public class FindDuplicate {
public static int FindDuplicateNaive(int[] input) {
for (int i = 0; i < input.length - 1; i++) {
for (int j = i + 1; j < input.length; j++) {
if (input[i] == input[j])
return input[i];
}
}
return -1;
}
public static int[] CreateTestCase(int n) {
// 1, 2, 3, 4, 5, 1 = n = 6
int[] output = new int[n];
int i;
for (i = 0; i < n - 1; i++) {
output[i] = i + 1;
}
output[i] = i;
return output;
}
public static void main(String[] args)
{
//Here also args[0] is 5,00,000
int number = Integer.parseInt(args[0]);
int[] input = CreateTestCase(number);
long start = System.currentTimeMillis();
int output = FindDuplicateNaive(input);
long end = System.currentTimeMillis();
System.out.println("Total time taken is: " + (end - start) / 1000.0 + " secs");
System.out.println(output);
}
我正在表中显示这些记录。在表u中,您将看到10条记录,并且在表的顶部将有一个按钮,因此,如果按下了添加按钮,则每次按下按钮时都必须添加10条记录,并且数据必须相同,但是必须使用以下内容附加逻辑我试图通过推10条记录并尝试将其附加到ex来设置状态,如果我按1,2,3则有1,2,3,4,5,6,7,8,9,10 ,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10必须被取消
state = {
Response: [
{
"id": "15071",
"name": "John",
"salary": "53",
"age": "23",
"department": "admin"
},
{
"id": "15072",
"name": "maxr",
"salary": "53",
"age": "23",
"department": "admin"
},
{
"id": "15073",
"name": "Josef",
"salary": "53",
"age": "23",
"department": "admin"
},
{
"id": "15074",
"name": "Ye",
"salary": "53",
"age": "23",
"department": "admin"
}
]
我在这里做错了什么
答案 0 :(得分:1)
如果我做对了,您正在尝试在this.state.employee
数组中添加10个对象的重复项,那么这些新对象和现有对象之间的唯一区别就是它们的id
。
如果是这种情况,请按照以下步骤操作:
appendEmployees() {
this.setState(prevState => {
// Get the biggest ID number.
const maxId = Math.max(...prevState.employee.map(e => parseInt(e.id)));
// create 10 new employees copies of the first 10.
const newEmployees = prevState.employee.slice(0, 10).map((e, i) => ({
...e,
id: (maxId + i + 1)
}));
// return/update the state with a new array for "employee" that is a concatenation of the old array and the array of the 10 new ones.
return {
employee: [...prevState.employee, ...newEmployees]
}
});
}
我在示例中添加了一些注释,以说明其功能。
重要的是this.setState
,它是用于更新状态的函数,在这里,我将其与函数一起用作第一个参数(它也适用于对象),我已经做到了因为这是从旧状态派生的生成新状态的首选方法。