logo
Tags down

shadow

Iterating over a list of types


By : sidibe
Date : October 16 2020, 11:12 AM
hope this fix your issue Your approach sounds reasonable to me (a combination of iterators/strategies).
code :


Share : facebook icon twitter icon

Iterating through object types in a list of parent type


By : AstroSnail
Date : March 29 2020, 07:55 AM
I wish did fix the issue. I am trying to iterate through a list of objects and only change the ones that match a particular type. My current code looks like this. (Platform is an extension of Entity, and entities is a list of type Entity) , Alright then :
code :
foreach (Platform p in entities.OfType<Platform>())
 //Will loop through all object of Platform type in entites.OfType<Platform>()

Iterating over a list and calling directives depending upon the item types , in angular js


By : Serdar Atakan
Date : March 29 2020, 07:55 AM
this will help ngIf or ngSwitch might be helpful here, with a few extra wrapping elements within the ngRepeat, in order to dynamically choose what to include based on Object.Type. Using ngSwitch:
code :
<ul>
  <li ng-repeat="object in List">
    <div ng-switch="object.Type">
      <div ng-switch-when="'1'">
        <div ng-include="object.TemplateName"></div>
      </div>
      <div ng-switch-when="'2'">
        <directive1 s-web-service-path="object.WebServicePath" >
        <h1>any html content</h1>
        </directive1>
      </div>
    </div>
  </li> 
</ul>

java - error incompatible types: unexpected return value - iterating through a list


By : user2557979
Date : March 29 2020, 07:55 AM
this will help I am asked to write code that will display the total sum of the integers in the numbers list. I am to make use of the total variable that has already been declared for me and use a for loop to iterate through the elements of the list. , Remove print around System.out.println(sum) and remove return sum
code :
import java.util.ArrayList;

public class ArrayListTestProgram {
    public static void main(String args[]) {
        int total = 0;
        ArrayList<Integer> numbers;
        numbers = new ArrayList<Integer>();
        numbers.add(1);
        numbers.add(45);
        numbers.add(23);
        numbers.add(87);
        numbers.add(89);
        numbers.add(213);
        System.out.println("The ArrayList looks like this: " + numbers);
        System.out.println("It has " + numbers.size() + " elements in it");
        System.out.println("The 5th element in it is: " + numbers.get(4));
        int sum = 0;


        for (int i = 0; i < numbers.size(); i++) {
            sum += numbers.get(i);

        }
        System.out.println(sum);

    }
}

Iterating through list and summing up all amounts where types are the same


By : Tweedskin
Date : March 29 2020, 07:55 AM
This might help you I need to add up all amounts in MonthlyExpenses list where types are equal. , You can achieve this by using streams and collectors:
code :
    private static List<MonthlyExpense> createMonthlyExpenses(List<MonthlyExpense> expenses) {
        Map<String, Double> sums = expenses.stream()
                .collect(Collectors.groupingBy(
                            MonthlyExpense::getType, 
                            Collectors.summingDouble(MonthlyExpense::getAmount))
        );
        return sums.entrySet().stream()
                .map(entry -> new MonthlyExpense(entry.getKey(), entry.getValue()))
                .sorted(Comparator.comparing(MonthlyExpense::getType))
                .collect(Collectors.toList());
    }
import java.util.*;
import java.util.stream.Collectors;

public class FinancialCalculator {

    static class MonthlyExpense {
        private String type;
        private double amount;

        public MonthlyExpense(String type, double amount) {
            this.type = type;
            this.amount = amount;
        }

        public String getType() { return type; }
        public double getAmount() { return amount; }
        public String toString() { return String.format("%s: %.2f", type, amount); }
    }

    private static List<MonthlyExpense> createMonthlyExpenses(List<MonthlyExpense> expenses) {
        Map<String, Double> sums = expenses.stream()
                .collect(Collectors.groupingBy(
                            MonthlyExpense::getType, 
                            Collectors.summingDouble(MonthlyExpense::getAmount))
        );
        return sums.entrySet().stream()
                .map(entry -> new MonthlyExpense(entry.getKey(), entry.getValue()))
                .collect(Collectors.toList());
    }

    public static void main(String[] args) {
        MonthlyExpense[] expenses = new MonthlyExpense[] {
                new MonthlyExpense("UTILITIES", 75),
                new MonthlyExpense("CREDIT", 1000),
                new MonthlyExpense("UTILITIES", 50),
                new MonthlyExpense("CREDIT", 2000),
                new MonthlyExpense("UTILITIES", 150),
                new MonthlyExpense("CAR", 344)
        };  
        System.out.println(createMonthlyExpenses(Arrays.asList(expenses)));
    }
}

Iterating a list of different data types?


By : l1z2e3i1
Date : March 29 2020, 07:55 AM
wish helps you I would seriously consider if this is the most appropriate design in this context. Are you sure the IMachine interface shouldn't have a Process method? Each machine could implement this as appropriate, and then the loop just becomes:
shadow
Privacy Policy - Terms - Contact Us © bighow.org