I have an Arraylist of integers.
My requirement is to determine if the arraylist HAS an element existing at the specified index.If YES, then a value should be set to that index (using set
method), else a value should be added to that index location(using add
method)
Finding it a bit difficult to handle the above condition in my java code.Please help.
Here's what I have so far:
ArrayList<Integer> tempArray = new ArrayList<Integer>();
int counter = 0;
int tempValue = 0;
For LOOP -
if (//certain conditions are satisfied){
tempValue = calculateNewValue();
tempArray.add(counter, tempValue); //Need some logic here to determine if its a set or add method to be used
}
if (//some other conditions are satisfied){
counter++;
}
end For LOOP
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…