我有一个类,我喜欢通过读取一个configfile来初始化我的var,它生成中间对象/ val,我想在一个方法中对它进行分组和隐藏.这是问题的最小部分 - 我用一个参数调用ctor,实际上是一个要解析的文件,而init-method生成String,实际上比这里更复杂,创建了很多中间对象:
class Foo (val i: Int) {
var s : String;
def init () {
s = "" + i
}
init ()
}
Run Code Online (Sandbox Code Playgroud)
这将产生错误:class Foo needs to be abstract, since variable s is not defined.在这个例子中,通过将String设置为""来很容易解决var s = "";,但实际上对象比String更复杂,没有适当的Null实现.
我知道,我可以使用一个Option,它也适用于比String更复杂的东西:
var s : Option [String] = None
def init () {
s = Some ("" + i)
}
Run Code Online (Sandbox Code Playgroud)
或者我可以省去我的方法电话.使用一个选项将迫使我一遍又一遍地写一些,没有太大的好处,因为没有必要除了以我认为可能的方式初始化它.
还有另一种方法来实现我的目标吗?
我想生成一个字符串列表的所有可能组合的列表(它实际上是一个对象列表,但为了简单起见,我们将使用字符串).我需要这个列表,以便我可以在单元测试中测试每个可能的组合.
例如,如果我有一个列表:
var allValues = new List<string>() { "A1", "A2", "A3", "B1", "B2", "C1" }
Run Code Online (Sandbox Code Playgroud)
我需要一个List<List<string>>所有组合,如:
A1
A2
A3
B1
B2
C1
A1 A2
A1 A2 A3
A1 A2 A3 B1
A1 A2 A3 B1 B2
A1 A2 A3 B1 B2 C1
A1 A3
A1 A3 B1
etc...
Run Code Online (Sandbox Code Playgroud)
递归函数可能是获得所有组合的方法,但它似乎比我想象的更难.
有什么指针吗?
谢谢.
编辑:两个解决方案,有或没有递归:
public class CombinationGenerator<T>
{
public IEnumerable<List<T>> ProduceWithRecursion(List<T> allValues)
{
for (var i = 0; i < (1 << allValues.Count); i++)
{
yield return ConstructSetFromBits(i).Select(n => allValues[n]).ToList(); …Run Code Online (Sandbox Code Playgroud) 我有以下代码,但我对所有的泛型都很困惑.
public static <T> List<T> backwards (List<? super T> input) {
List<T> output = new ArrayList<T>();
return output;
}
Run Code Online (Sandbox Code Playgroud)
我的理解是我有一个名为public的方法backwards,它创建一个实现List接口并返回arraylist的arraylist.我的问题是我实际上对编译器说的是以下部分......
static <T> List<T> backwards (List<? super T> input)
Run Code Online (Sandbox Code Playgroud) 如何更改此var?
max=0;
min=20000000;
cat |while read
do
read a
if [[ $a -gt $max ]]
then
max=a`
fi
`if [[ $a -lt $min ]]
then
min=a
fi
done
echo $max
echo $min
Run Code Online (Sandbox Code Playgroud)
我的最小值和最大值仍然相同,0和2000000.任何人都可以帮我这个吗?我不知道.
是否可以使用PHP识别Linux 32或64位?
phpinfo()
Run Code Online (Sandbox Code Playgroud)
回报
Linux infong 2.4 #1 SMP Mon Oct 10 09:34:36 UTC 2011 i686 GNU/Linux
Run Code Online (Sandbox Code Playgroud)
它是共享主机,所以我不能使用命令行.
我目前正在学习Java,我遇到了这个问题,我想加载一个包含大量行的文件(我逐行读取文件),我想要做的就是跳过某些行(伪代码) ).
the line thats starts with (specific word such as "ABC")
Run Code Online (Sandbox Code Playgroud)
我试过用
if(line.startwith("abc"))
Run Code Online (Sandbox Code Playgroud)
但那没用.我不确定我做错了,这就是为什么我在这里寻求帮助,在加载功能的一部分下面:
public String loadfile(.........){
//here goes the variables
try {
File data= new File(dataFile);
if (data.exists()) {
br = new BufferedReader(new FileReader(dataFile));
while ((thisLine = br.readLine()) != null) {
if (thisLine.length() > 0) {
tmpLine = thisLine.toString();
tmpLine2 = tmpLine.split(......);
[...]
Run Code Online (Sandbox Code Playgroud) 我有一个非常具体的问题,与内部类有关.让我给你看一些示例代码:
class Foo {
MYOPTIONS temp;
public static enum MYOPTIONS {
OPTION1, OPTION2, OPTION3;
}
}
Run Code Online (Sandbox Code Playgroud)
所以这个枚举在Foo类中.现在我要做的是将temp变量设置为三个选项之一,但是在类Foo之外执行,比如说来自一个名为External的类.不幸的是我无法使用set方法来执行此操作,因为External.setTemp (MYOPTIONS.OPTION1)无效,因为枚举在类外部不可见.所以我唯一能想到的就是在Foo类中有三个方法:
public void setTempOption1 () {this.temp=MYOPTIONS.OPTION1;}
public void setTempOption2 () {this.temp=MYOPTIONS.OPTION2;}
public void setTempOption3 () {this.temp=MYOPTIONS.OPTION3;}
Run Code Online (Sandbox Code Playgroud)
显然,另一种选择是更改枚举,而不是将其作为内部类.我还缺少其他选择吗?谢谢
我试图在java中复制文件并将其移动到新文件夹.这是我一直在使用的代码,但我总是在指定的目录中收到此错误"(访问被拒绝)".有没有办法可以解决这个或更好的方法来复制文件?谢谢
try{
File f1 = new File(fpath);
File f2 = new File("C:/users/peter/documents/foldertest2/hats");
InputStream in = new FileInputStream(f1);
//For Append the file.
//OutputStream out = new FileOutputStream(f2,true);
//For Overwrite the file.
OutputStream out = new FileOutputStream(f2);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0){
out.write(buf, 0, len);
}
in.close();
out.close();
System.out.println("File copied.");
}
catch(FileNotFoundException ex){
System.out.println(ex.getMessage() + " in the specified directory.");
System.exit(0);
}
catch(IOException e){
System.out.println(e.getMessage());
}
Run Code Online (Sandbox Code Playgroud)
更新:我检查了文件夹权限,它们对所有用户和我的所有用户都是开放的
scala> val s = 7.toBinayString
<console>:7: error: value toBinayString is not a member of Int
val s = 7.toBinayString
^
scala> val k = 7
k: Int = 7
scala> k.toBinaryString
res44: String = 111
Run Code Online (Sandbox Code Playgroud)
由于val k = 7和7被标识为Int,而不是RichInt或java.lang.Integer,我不明白为什么它们的处理方式不同.
为什么会有区别?