Skip to main content
Code Review

Return to Answer

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

A possible future improvement would be to generate the timePeriods with a method method. More complicated the first time that you write it, but easier to use in the future.

A possible future improvement would be to generate the timePeriods with a method. More complicated the first time that you write it, but easier to use in the future.

A possible future improvement would be to generate the timePeriods with a method. More complicated the first time that you write it, but easier to use in the future.

Source Link
mdfst13
  • 22.4k
  • 6
  • 34
  • 70

I want to know if these is a simple way to remove all of the nested for-loops and do this task more elegantly, whilst retaining simplicity.

Yes, for some definition of simplicity. Consider

public class DataCreator implements Iterable<String> {
 private final List<List<String>> parts;
 private final List<String> lines = new ArrayList<>();
 private final String SEPARATOR;
 private DataCreator(List<List<String>> parts, String separator) {
 this.parts = parts;
 this.SEPARATOR = separator;
 }
 private void joinParts(int index, String line) {
 if (index >= parts.size()) {
 lines.add(line + lines.size());
 return;
 }
 for (String part : parts.get(index++)) {
 joinParts(index, line + part + SEPARATOR);
 }
 }
 @Override
 public Iterator<String> iterator() {
 return lines.iterator();
 }
 public static DataCreator create(List<List<String>> parts, String separator) {
 DataCreator creator = new DataCreator(parts, separator);
 creator.joinParts(0, new String());
 return creator;
 }
 public static void main(String[] args) {
 String[] timePeriods = 
 { 
 "2001年01月01日", "2001年01月02日", "2001年01月03日", "2001年01月04日", "2001年01月05日", 
"2001年01月06日", "2001年01月07日", "2001年01月08日", "2001年01月09日", "2001年01月10日", "2001年01月11日", 
"2001年01月12日", "2001年01月13日", "2001年01月14日", "2001年01月15日", "2001年01月16日", "2001年01月17日", 
"2001年01月18日", "2001年01月19日", "2001年01月20日", "2001年01月21日", "2001年01月22日", "2001年01月23日", 
"2001年01月24日", "2001年01月25日", "2001年01月26日", "2001年01月27日", "2001年01月28日", "2001年01月29日", 
"2001年01月30日", "2001年01月31日",
 "2001年02月01日", "2001年02月02日", "2001年02月03日", "2001年02月04日", "2001年02月05日", 
"2001年02月06日", "2001年02月07日", "2001年02月08日", "2001年02月09日", "2001年02月10日", "2001年02月11日", 
"2001年02月12日", "2001年02月13日", "2001年02月14日", "2001年02月15日", "2001年02月16日", "2001年02月17日", 
"2001年02月18日", "2001年02月19日", "2001年02月20日", "2001年02月21日", "2001年02月22日", "2001年02月23日", 
"2001年02月24日", "2001年02月25日", "2001年02月26日", "2001年02月27日", "2001年02月28日",
 "2001年03月01日", "2001年03月02日", "2001年03月03日", "2001年03月04日", "2001年03月05日", 
"2001年03月06日", "2001年03月07日", "2001年03月08日", "2001年03月09日", "2001年03月10日", "2001年03月11日", 
"2001年03月12日", "2001年03月13日", "2001年03月14日", "2001年03月15日", "2001年03月16日", "2001年03月17日", 
"2001年03月18日", "2001年03月19日", "2001年03月20日", "2001年03月21日", "2001年03月22日", "2001年03月23日", 
"2001年03月24日", "2001年03月25日", "2001年03月26日", "2001年03月27日", "2001年03月28日", "2001年03月29日", 
"2001年03月30日", "2001年03月31日"
 };
 List<List<String>> parts = new ArrayList<>();
 parts.add(Arrays.asList("DD1", "DD2", "DD3", "DD4"));
 parts.add(Arrays.asList("1A", "1B", "1C", "1D", "1E", "1F", "1G", "1H", 
 "1J", "1K", "1L", "1M", "1N", "1O", "1P", "1Q"));
 parts.add(Arrays.asList("AB1", "ZZ2", "FAQWE1", "1234", "_Z"));
 parts.add(Arrays.asList("D"));
 parts.add(Arrays.asList(timePeriods));
 for (String line : DataCreator.create(parts, ", ")) {
 System.out.println(line);
 }
 }
}

This is slightly more complicated, but it uses it for making the code more generic and reusable.

I used the factory method (create) because I don't like doing complicated work in the constructor. This leaves the code more flexible for later modification.

Advantages over the original:

  • Easy to add or remove columns. Just add or delete the appropriate parts.add line.
  • Reusable. Capable of doing things other than immediate display.
  • Easy to use. Because it implements Iterable, you can just throw it into a for loop and use it.
  • Divided into methods.
  • Defines SEPARATOR at construction time rather than hardcoding it.

Downsides retained include that you have to edit code to change the format of the strings. For example, if you wanted the row number to be in the first column instead of the last, you would have to modify the code.

I left main inside of DataCreator for simplicity's sake. Best practices would suggest putting it in its own class so that DataCreator would be more reusable.

A possible future improvement would be to generate the timePeriods with a method. More complicated the first time that you write it, but easier to use in the future.

lang-java

AltStyle によって変換されたページ (->オリジナル) /