-1

I have an ArrayList where I want to call two methods on the first two objects in the list, and different methods on the rest, how can I do this the easiest way?

So far I have this

ArrayList<Material> materials = new ArrayList();
StyklisteMetodeKlasse.fillArray(materials);

for(Material materialer: materials.subList(0, 1)){  
    int brugerInput = 0; // this is only a temporary varible
    materialer.setAmount(Material.calculatePlanks(brugerInput, materialer.getLength()));
    materialer.setAmount(Material.calculatePlanks(brugerInput, materialer.getLength()));
//here is some code where i call different methods on the rest of the materials

When I call a method on the "materialer" does it apply for all the objects or just the first, then the second?

12
  • materials.subList(0, 1) gives you a sublist with only the first element. So in this case you're only calling setAmount for the first element. But if you change it to subList(0, 2), you'll be calling setAmount for the first two elements. Commented May 10, 2018 at 23:51
  • 1
    Is it always just the first two elements in the list that you want to call method A() on, and the rest on which you want to call method B() on, without any additional criteria? If so, why not just split the list? Commented May 10, 2018 at 23:53
  • okay so i should just change subList to however elements the method should be called one? I would much rather use the non-enhanced for loop but then i can't get objects... Is there any pther design patterns, that i could implement? Commented May 10, 2018 at 23:54
  • You 'would much rather use the non-enhanced for loop' why? And what makes you think you can't get objects with it? Commented May 10, 2018 at 23:56
  • @user991710 so i have an ArrayList of "materials", where i need to calculate the amount needed for a certain length, but the methods used are different for each material, so i think it would be easier to use just one list, and the make sublists of the list, and call each method? Commented May 10, 2018 at 23:57

1 Answer 1

0

The best approach would most likely be the simplest one. Using polymorphism, and try and get the type of the object at runtime and select what you need to do, would be a sleek solution, but as you said, it might get complicated, especially if you do not have control over the structure and nature of the objects being passed to you.

Alternatively, you could make your classes implement an interface which abstracts the operation that you would need to do. This would allow you to always call the same method, without having to worry about who is what.

As it has been pointed out in the comments, having hardcoded index values could potentially cause more trouble than it will ever solve, since it assumes that who ever is consuming your method has inside knowledge of it how specifically works, as opposed to what it should do.

Most likely, the best approach would be to change your method to take 2 lists, as opposed to 1. This approach is easier to understand and also gives you more control and has you make less assumptions, which is usually always a good thing.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.