I want to add some values in arraylist but at random index. Is this possible?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You could use Math.Random with a range to do this. I doubt you want to be adding it at ANY index because ArrayList is a dynamically expanding array which means that it will have to resize if you use indexes beyond a certain size. This will make your inserts more costly than they need to be and it will also take up more memory than needed. If you know you’ll have only say.. 100 items then just pick a random value between 1 – 100.
EDIT: Random doesn’t produce decimal values if you do the following.. you say you want a range between 1-4 than the following should give you what you want…