-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?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
kristheman
  • 89
  • 1
  • 13
  • `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. – k_ssb May 10 '18 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? – filpa May 10 '18 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? – kristheman May 10 '18 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? – user207421 May 10 '18 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? – kristheman May 10 '18 at 23:57
  • @EJP so should i do a regular foor loop and then get index i, and call the methods accordingly? – kristheman May 11 '18 at 00:00
  • Probably the best way to solve this then is to use polymorphism. Are you familiar with the concept? Each type of material might extend a class `Material`: `MaterialA`, `MaterialB`, ... . Then, at runtime, the proper method would get called depending on the actual type of the object. – filpa May 11 '18 at 00:00
  • @user991710, yes i'm familiar.. i have tried the concept, but it get to complex, it ended up being more difficult for other aspects of the program. – kristheman May 11 '18 at 00:04
  • Well, you've only posted this bit of code. We can't guess at what potential effects our comments and answers might have on unseen pieces of code :) – filpa May 11 '18 at 00:07
  • thats true, would it be easier to make two different objects, and seperate lists for each object (i have a list of about 22 objects, and three different types). I need to print the object, along with the amount in the end. – kristheman May 11 '18 at 00:11
  • Generalize it to a method all the types can override. As soon as you get into caring about what specific type you have, or worse - what index you have, your code becomes fragile. – ChiefTwoPencils May 11 '18 at 02:04
  • Possible duplicate of [Java, call object methods thorugh arraylist](https://stackoverflow.com/questions/18435992/java-call-object-methods-thorugh-arraylist) – Venki WAR May 11 '18 at 05:05

1 Answers1

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.

npinti
  • 51,780
  • 5
  • 72
  • 96