我有这个Java代码,将用户年龄分为8组:
private int[] A0, A1, A2, A3, A4, A5, A6, A7, A8;
public Grouping() {
super();
for (int i = 5; i < 10; i++)
A0[i] = i;
for (int i = 10; i < 20; i++)
A1[i] = i;
for (int i = 20; i < 30; i++)
A2[i] = i;
for (int i = 30; i < 40; i++)
A3[i] = i;
for (int i = 40; i < 50; i++)
A4[i] = i;
for (int i = 50; i < 60; i++)
A5[i] = i;
for (int i = 60; i < 70; i++)
A6[i] = i;
for (int i = 70; i < 80; i++)
A7[i] = i;
for (int i = 10; i < 20; i++)
A8[i] = i;
}
现在我想优化这段代码。有什么想法吗?
答案 0 :(得分:4)
You've said你想要它&#34;更短。&#34;这是一个较短的版本,除了我不知道您尝试使用A8
做什么,这对A1
来说是多余的,因此将其关闭:
private int[][] groups; // I assume you initialize `A0` and so on somewhere you haven't shown; initialize this there
public Grouping() {
int i, groupIndex, group;
super();
groupIndex = 0;
group = groups[0];
for (i = 5; i < 80; ++i) {
if (i % 10 == 0) {
++groupIndex;
group = groups[groupIndex];
}
group[i] = i;
}
}
请注意,这仍然是相当奇怪的代码,因为它执行原始代码所做的操作,并将数组中的所有类型的条目保留为其默认值(0
)。例如,在您的代码中,A0
的索引0
到4
永远不会被分配到groups[0]
&#39 ; s 0
到4
也是如此。 (随后的差距会更大。)
你的标题,但是,你说它想要更多&#34;高性能。&#34; &#34;高性能&#34; !=&#34;更短&#34;您的代码可能执行就好了,可能比上面的内容更难以理解,因为它不需要%
操作和循环内的if
。
或者这是一个实施Seelenvirtuose's suggestion的版本:
private int[][] groups; // I assume you initialize `A0` and so on somewhere you haven't shown; initialize this there
public Grouping() {
int i;
super();
for (i = 5; i < 80; ++i) {
groups[i / 10][i] = i;
}
}
...因为i / 10
位是整数除法而起作用。
答案 1 :(得分:0)
创建一个包含年龄范围的组类和一个用于保存该组匹配的计数器。将组对象添加到列表中并进行游戏。
public static void main(String[] args) {
List<Group> lists = new ArrayList<Group>(10);
lists.add(new Group(5, 10));
lists.add(new Group(10, 20));
lists.add(new Group(20, 30));
lists.add(new Group(30, 40));
int age[] = { 5, 10, 20, 30, 11, 22, 33 };
for (int i = 0; i < age.length; i++)
grouper: for (Group group : lists)
if (group.validateAgeGroup(age[i]))
break grouper;
for (Group group : lists)
System.out.println(group);
}
小组课程:年龄范围和计数器:
public class Group {
int startAge, endAge, counter;
public Group(int startAge, int endAge) {
this.startAge = startAge;
this.endAge = endAge;
}
public boolean validateAgeGroup(int age) {
if (age >= startAge && age < endAge) {
this.setCounter(1);
return true;
}
return false;
}
public int getCounter() {
return counter;
}
public int getEndAge() {
return endAge;
}
public int getStartAge() {
return startAge;
}
public void setCounter(int counter) {
this.counter += counter;
}
public void setEndAge(int endAge) {
this.endAge = endAge;
}
public void setStartAge(int startAge) {
this.startAge = startAge;
}
@Override
public String toString() {
return "Group [startAge=" + getStartAge() + ", endAge=" + getEndAge()
+ ", counter=" + getCounter() + "]";
}
}
答案 2 :(得分:0)
我使用java8流。请参阅我的示例代码:
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class Grouping {
public static void main(String[] args) {
// create a stream from 5 (inclusive) to 80 (exclusive)
IntStream.range(5, 80)
// convert it to Integer objects
.boxed()
// group it by tens
.collect(Collectors.groupingBy(i -> i / 10))
// iterate over each group
.entrySet().stream()
// pretty format
.map(entry -> "A"+entry.getKey()+"="+entry.getValue())
// print to console
.forEach(System.out::println);
}
}
输出:
A0=[5, 6, 7, 8, 9]
A1=[10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
A2=[20, 21, 22, 23, 24, 25, 26, 27, 28, 29]
A3=[30, 31, 32, 33, 34, 35, 36, 37, 38, 39]
A4=[40, 41, 42, 43, 44, 45, 46, 47, 48, 49]
A5=[50, 51, 52, 53, 54, 55, 56, 57, 58, 59]
A6=[60, 61, 62, 63, 64, 65, 66, 67, 68, 69]
A7=[70, 71, 72, 73, 74, 75, 76, 77, 78, 79]