整数求和通过合并

huangapple go评论69阅读模式
英文:

Sum of Ints by merging

问题

public class SomeClass {

    int a = 0;
    int b = 0;
    int c = 0;
    // ...

    public void mergeValues(final SomeClass other) {

        this.a += other.a;
        this.b += other.b;
        this.c += other.c;
        // .....

        return this;
    }
}
英文:

The essence:

There is a class with int fields (from 1 to infinity). This class has a method that adds these values ​​with another instance of this class. Question: Is it possible to do it somehow more elegantly?

Code:

public class SomeClass {

	int a = 0;
	int b = 0;
	int c = 0;
	...

	public void mergeValues(final SomeClass other) {
	
		this.a += other.a;
		this.b += other.b;
		this.c += other.c;
		.....
	 
		return this;
	}
}

答案1

得分: 1

你可以将这些整数存储在一个映射中。因此,你从字符/字符串映射到整数。然后,对于另一个类映射中的每个键,你将该值添加到该类映射中相应值的位置。类似以下示例。我已经有一段时间没有使用过 Java,但是思想应该是相同的。

for (Map.Entry<Integer, String> set : other.getMap().entrySet()){
    this.map.put(set.getKey(), set.getValue() + this.map.get(set.getKey()));
}

另外,你也可以使用反射来迭代遍历所有字段。请参阅这里

英文:

You can store these integers in a map. So you map from char/string to int. So then for each key in the other class's map, you add that value to the corresponding value in this class's map. Something like the following. It's been a while since I've used java, but the idea should be the same.

for (Map.Entry&lt;Integer, String&gt; set : other.getMap().entrySet()){
    this.map.put(set.getKey(), set.getValue()+ this.map.get(set.getKey()))
}

Otherwise, you can use reflection to iterate over all of the fields. See here.

答案2

得分: 1

Arrays.stream(getClass().getDeclaredFields()).forEach(n -> {
            try {
                    long temp = n.getLong(this);
                    temp += oc.getClass().getDeclaredField(n.getName()).getLong(oc);
                    if (n.getType().isAssignableFrom(int.class)) {
                        n.setInt(this, (int) temp);
                    } else if (n.getType().isAssignableFrom(long.class)) {
                        n.setLong(this, temp);
                    }

            } catch (IllegalAccessException | NoSuchFieldException e) {
                e.printStackTrace();
            }
        });
英文:
Arrays.stream(getClass().getDeclaredFields()).forEach(n -&gt; {
            try {
                    long temp = n.getLong(this);
                    temp += oc.getClass().getDeclaredField(n.getName()).getLong(oc);
                    if (n.getType().isAssignableFrom(int.class)) {
                        n.setInt(this, (int) temp);
                    } else if (n.getType().isAssignableFrom(long.class)) {
                        n.setLong(this, temp);
                    }

            } catch (IllegalAccessException | NoSuchFieldException e) {
                e.printStackTrace();
            }
        });

huangapple
  • 本文由 发表于 2020年9月14日 11:09:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/63877584.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定