java - Method which takes two ArrayLists as parameters -
I am completing an exercise which tells me to write a method called Total Tex, which is a form of two ArrayLists parameters In other cases, there are shops, others return the total tax payable on all those properties, and the house has two classes in my program. Each has a method that pays tax as an integer.
My solution is as follows, but I'm not sure that this is the most effective way of doing it, due to repetition, and it does not compile! Is there a way to get the same thing, but avoid the duplicate code, like I have?
Private Intel Kultex (Array List & Lt; Shop & gt; List 1, Arreist & Lt; House & gt; List 2) {int total; For (int a = 0; a & lt; = list1.size () -1; a ++) {total + = list1.shopTax (); } For (int a = 0; a & lt; = list2.size () -1; a ++) {total + = list2.houseTax (); } Total Return; }
Your solution will not work because you have access to these elements List , you are retrieving tax from the list, as shown here:
total + = list1.shopTax ( ); should be
total + = list1.get (a). ShopTax (); Here's the same:
total + = list2.houseTax (); It should be:
total + = list2.get (a) .houseTax (); However, this approach is using your ArrayList as a cover for an array. Another option can be treated as ArrayList as a List , so you can get using Iterator There are two ways to use the method Iterator :
- Announce
eaterand call itlist # iterator < / Code>:iterator & lt; Shop & gt; ShopIterator = list1.iterator (); While (shopIterator.hasNext ()) {store shop = shopIterator.next (); Total + = shop.shopTax (); } Iterator & lieutenant; House & gt; HouseIterator = list2.iterator (); While (houseIterator.hasNext ()) {home home = home eteter.exe (); Total + = house.shopTax (); } -
for extended
loop:(store shop: list 1 } {Total + = Shop.shopTax (); } (House House: List 2) {Total + = house.houseTax (); }
Comments
Post a Comment