有没有一种有效的方法可以找到 Java 中多个枚举之间所有可能的组合?
考虑以下三个枚举 -
public enum EnumOne {
One ("One"),
OneMore ("OneMore");
}
public enum EnumTwo {
Two ("Two"),
}
public enum EnumThree {
Three ("Three"),
ThreeMore ("ThreeMore");
}
我希望输出在这些多个枚举之间产生所有可能的组合,即
{EnumOne.One, EnumTwo.Two, EnumThree.Three},
{EnumOne.One, EnumTwo.Two, EnumThree.ThreeMore},
{EnumOne.OneMore, EnumTwo.Two, EnumThree.Three},
{EnumOne.OneMore, EnumTwo.Two, EnumThree.ThreeMore}
希望找到有效的处理方法。
谢谢
最佳答案
算法的复杂度是 O(NxMxK .... xZ) 如果我错了,我不知道这是否是一种“有效的方法”....我用回溯解决方案
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class ProductEnums {
public enum EnumOne {
One,
OneMore;
}
public enum EnumTwo {
Two,
}
public enum EnumThree {
Three,
ThreeMore;
}
public static void main(String[] args) {
// pass each values in enums
List a = product(EnumOne.values(),
EnumTwo.values(), EnumThree.values());
System.out.println(a);
}
public static List<List<Enum>> product(Enum[]... enums) {
return product(new ArrayList<>(Arrays.asList(enums)));
}
public static List<List<Enum>> product(List<Enum[]> enums) {
if (enums.isEmpty()) {
//Trivial case of recursive function
return new ArrayList<>();
}
//remove first element
Enum[] myEnums = enums.remove(0);
List<List<Enum>> out = new ArrayList<>();
for (Enum e : myEnums) {
//call recursive
List<List<Enum>> list = product(enums);
for (List<Enum> list_enum : list) {
//for each list get from recursion adding element e
list_enum.add(0, e);
out.add(list_enum);
}
if(list.isEmpty()){
List<Enum> list_enum = new ArrayList<>();
list_enum.add(e);
out.add(list_enum);
}
}
enums.add(0, myEnums); //Backtraking
return out;
}
}
结果
[[One, Two, Three], [One, Two, ThreeMore], [OneMore, Two, Three], [OneMore, Two, ThreeMore]]
关于java - 查找所有可能的枚举组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29993586/
我试图获取一个长度在1到10之间的字符串,并输出将字符串分解为大小为1、2或3的连续子字符串的所有可能方式。例如:输入:123456将整数分割成单个字符,然后继续查找组合。该代码将返回以下所有数组。[1,2,3,4,5,6][12,3,4,5,6][1,23,4,5,6][1,2,34,5,6][1,2,3,45,6][1,2,3,4,56][12,34,5,6][12,3,45,6][12,3,4,56][1,23,45,6][1,2,34,56][1,23,4,56][12,34,56][123,4,5,6][1,234,5,6][1,2,345,6][1,2,3,456][123
我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/
当我的预订模型通过rake任务在状态机上转换时,我试图找出如何跳过对ActiveRecord对象的特定实例的验证。我想在reservation.close时跳过所有验证!叫做。希望调用reservation.close!(:validate=>false)之类的东西。仅供引用,我们正在使用https://github.com/pluginaweek/state_machine用于状态机。这是我的预订模型的示例。classReservation["requested","negotiating","approved"])}state_machine:initial=>'requested
我有这个html标记:我想得到这个:我如何使用Nokogiri做到这一点? 最佳答案 require'nokogiri'doc=Nokogiri::HTML('')您可以通过xpath删除所有属性:doc.xpath('//@*').remove或者,如果您需要做一些更复杂的事情,有时使用以下方法遍历所有元素会更容易:doc.traversedo|node|node.keys.eachdo|attribute|node.deleteattributeendend 关于ruby-Nokog
我刚刚被困在这个问题上一段时间了。以这个基地为例:moduleTopclassTestendmoduleFooendend稍后,我可以通过这样做在Foo中定义扩展Test的类:moduleTopmoduleFooclassSomeTest但是,如果我尝试通过使用::指定模块来最小化缩进:moduleTop::FooclassFailure这失败了:NameError:uninitializedconstantTop::Foo::Test这是一个错误,还是仅仅是Ruby解析变量名的方式的逻辑结果? 最佳答案 Isthisabug,or
我想获取模块中定义的所有常量的值:moduleLettersA='apple'.freezeB='boy'.freezeendconstants给了我常量的名字:Letters.constants(false)#=>[:A,:B]如何获取它们的值的数组,即["apple","boy"]? 最佳答案 为了做到这一点,请使用mapLetters.constants(false).map&Letters.method(:const_get)这将返回["a","b"]第二种方式:Letters.constants(false).map{|c
我正在尝试解析一个CSV文件并使用SQL命令自动为其创建一个表。CSV中的第一行给出了列标题。但我需要推断每个列的类型。Ruby中是否有任何函数可以找到每个字段中内容的类型。例如,CSV行:"12012","Test","1233.22","12:21:22","10/10/2009"应该产生像这样的类型['integer','string','float','time','date']谢谢! 最佳答案 require'time'defto_something(str)if(num=Integer(str)rescueFloat(s
我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www
我只想对我一直在思考的这个问题有其他意见,例如我有classuser_controller和classuserclassUserattr_accessor:name,:usernameendclassUserController//dosomethingaboutanythingaboutusersend问题是我的User类中是否应该有逻辑user=User.newuser.do_something(user1)oritshouldbeuser_controller=UserController.newuser_controller.do_something(user1,user2)我
什么是ruby的rack或python的Java的wsgi?还有一个路由库。 最佳答案 来自Python标准PEP333:Bycontrast,althoughJavahasjustasmanywebapplicationframeworksavailable,Java's"servlet"APImakesitpossibleforapplicationswrittenwithanyJavawebapplicationframeworktoruninanywebserverthatsupportstheservletAPI.ht