[英]Java program for package installer
May you please help in writing a java program which should accept an array of strings defining dependencies.能否请您帮忙编写一个 java 程序,该程序应该接受定义依赖项的字符串数组。 Each string contains the name of a package followed by a colon and space, then any dependencies required by that package.
每个字符串包含一个包的名称,后跟一个冒号和空格,然后是该包所需的任何依赖项。 For simplicity we'll assume a package can have at most one dependency.
为简单起见,我们假设一个包最多可以有一个依赖项。 The program should output a comma separated list of package names in the order of install, such that a package's dependency will always precede that package.
程序应该按照安装的顺序输出一个逗号分隔的包名列表,这样一个包的依赖将总是在那个包之前。 The program should reject as invalid a dependency specification that contains cycles.
程序应该拒绝包含循环的依赖项规范无效。
Example of valid input KittenService: Leetmeme: Cyberportal Cyberportal: Ice CamelCaser: KittenService Fraudstream: Leetmeme Ice:有效输入示例 KittenService: Leetmeme: Cyberportal Cyberportal: Ice CamelCaser: KittenService Fraudstream: Leetmeme Ice:
A valid output for the above would be:上述的有效输出是:
KittenService, Ice, Cyberportal, Leetmeme, CamelCaser, Fraudstream KittenService, Ice, Cyberportal, Leetmeme, CamelCaser, Fraudstream
Example of input that should be rejected (contains cycles) KittenService: Leetmeme: Cyberportal Cyberportal: Ice CamelCaser: KittenService Fraudstream: Ice: Leetmeme应该被拒绝的输入示例(包含循环) KittenService: Leetmeme: Cyberportal Cyberportal: Ice CamelCaser: KittenService Fraudstream: Ice: Leetmeme
First of all: SO is not a coding-service.首先:SO 不是编码服务。 Show us what you've got and where the problem lies and you'll get help with it.
向我们展示您有什么以及问题出在哪里,您将获得帮助。 Now for the solution - I won't code anything and I hope noone else does.
现在的解决方案 - 我不会编码任何东西,我希望没有其他人这样做。 Since the dependencies don't contain any cycles, the structure is a tree.
由于依赖项不包含任何循环,因此结构是一棵树。 Just traverse the tree postorder and import each package in this order.
只需遍历树后序并按此顺序导入每个包。
Get Package List from input String Array:从输入字符串数组中获取包列表:
public static List<String> getPackages(String[] intputStrings,
String inputDelimiter, char wordEndChar, String rejectWord,
String outputDelimiter) {
List<String> al = new ArrayList<String>();
for (String inputString : intputStrings) {
if (!(inputString.indexOf(rejectWord) > 0)) {
for (String string : inputString.split(inputDelimiter)) {
int idx = string.indexOf(wordEndChar);
if (idx > 1)
al.add(string.substring(0, idx));
}
}
}
return al;
}
Invocation Part (Input and output) :调用部分(输入和输出):
public static void main(String[] args) {
String intputString[] = {
"KittenService: Leetmeme: Cyberportal Cyberportal: Ice CamelCaser: KittenService Fraudstream: Ice: Leetmeme",
"(contains cycles) KittenService: Leetmeme: Cyberportal Cyberportal: Ice CamelCaser: KittenService Fraudstream: Ice: Leetmeme",
"One: Two: x Three: Ice CamelCaser: KittenService Fraudstream: Five: Leetmeme" };
List<String> s = getPackages(intputString, " ", ':', " cycles", ", ");
for (String string : s)
System.out.print(string + " ,");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.