• Post Reply Bookmark Topic Watch Topic
  • New Topic

inserting a new value to an arrayList at the current position in the index  RSS feed

 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I've finally managed to get my arrayList based program working, but there is one small item missing. When a user goes through the arrayList, forwards and backwards and then enters a new value it is automatically added to the end of the arrayList.

Does anybody know if there is a way to add this value to the current position in the list?

eg: a user enters 12,21,31,41,51 and then uses the index of these values to go back through the list to 21. They then enter a new value, 53, and want it saved after 21 but before 31.

What would be the best way to achieve this result?

My code for adding to the arrayList is currently as follows,

public class ClickAction implements ActionListener {

public void actionPerformed(ActionEvent event) {

// Parse degrees Celsius as a double
// and convert to Fahrenheit.

String celsiusAsText = tempCelsius.getText();
try { //start of the exception handling
Double celsiusAsDouble = Double.parseDouble(celsiusAsText);
double fahrenheitAsDouble = celsiusAsDouble*1.8+32;
fahrenheitLabel.setText(fahrenheitAsDouble + " Fahrenheit");
celsiusList.add(celsiusAsDouble);
fahrenheitList.add(fahrenheitAsDouble);
celsiusListCurrentIndex = celsiusList.size() - 1;//end of the list
fahrenheitListCurrentIndex = fahrenheitList.size() - 1;//end of the list
} catch(Exception e)
{fahrenheitLabel.setText("? Fahrenheit");}
}
}



Any advice or help appreciated!
 
Ranch Hand
Posts: 2410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One approach you might try is to add an empty element at the end of the ArrayList, that is an empty String or an empty Object. Then beginning at the end of the list, use the set method of ArrayList to move each element to one position ahead of where it currently is. Once you have reached the position where you want to add the new element, use the set method to put the element there.
 
celine scarlett
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Thanks for the reply. I've tried using an add method which allows you to specify the index number where you want to a new value, but with this program I have no way to know the index number when a user wants to add a new value. All I have is the 'int current index' value.

I'm assuming I can use this with the add method. I've tried using it in the actionPerformed method, but it produces an addition after every click of the backwards button etc.

What would be the best way to add this method to this program? I thought about adding it using an if statement, but I couldn't think of an effective point to change it to the else statement.

Any ideas or advice appreciated!
 
celine scarlett
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I've got the arrayList working now with the correct add method, except for one small problem. When a user adds a new value to the arrayList, it is added at the position before the current position. The other way of looking at this problem, is simply that the original value is moved forward one position in the arrayList, instead of the new value being placed after the current position.

eg: a user enters 10,20,30,40 and then goes back through the arrrayList to 20. Then they enter 25. This should be placed between 20 and 30, and not between 10 and 20 as is currently happening.

I've tried numerous different options but none produce the desired end result.

My current code is as follows,

Any advice or help appreciated.

Many thanks
 
celine scarlett
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Problem all solved now. I can eventually get some sleep.

Thanks to all for their help.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!