我试图将范围(0到9)中的所有数字相加,可以除以3或5。
方法1:
result
打印23,这是预期的。
方法2:与上述相同。但我试图忽略临时变量 println (0..9).findAll {
(it % 3 == 0 || it % 5 == 0)
}.sum()
&直接打印。
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
打印 def lst = 0..9
println lst.findAll {
(it % 3 == 0 || it % 5 == 0)
}.sum()
这里发生了什么?为什么显示整个列表而不是方法2中的总和。
方法3:与方法相同2.直接打印。但是将范围移动到变量。
public class Send {
public static void main(String[] args) throws Exception {
Socket socket = new Socket("localhost", 13085);
OutputStream outputStream = socket.getOutputStream();
BufferedImage image = ImageIO.read(new File("C:\\Users\\Jakub\\Pictures\\test.jpg"));
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ImageIO.write(image, "jpg", byteArrayOutputStream);
byte[] size = ByteBuffer.allocate(4).putInt(byteArrayOutputStream.size()).array();
outputStream.write(size);
outputStream.write(byteArrayOutputStream.toByteArray());
outputStream.flush();
System.out.println("Flushed: " + System.currentTimeMillis());
Thread.sleep(120000);
System.out.println("Closing: " + System.currentTimeMillis());
socket.close();
}
}
public class Receive {
public static void main(String[] args) throws Exception {
ServerSocket serverSocket = new ServerSocket(13085);
Socket socket = serverSocket.accept();
InputStream inputStream = socket.getInputStream();
System.out.println("Reading: " + System.currentTimeMillis());
byte[] sizeAr = new byte[4];
inputStream.read(sizeAr);
int size = ByteBuffer.wrap(sizeAr).asIntBuffer().get();
byte[] imageAr = new byte[size];
inputStream.read(imageAr);
BufferedImage image = ImageIO.read(new ByteArrayInputStream(imageAr));
System.out.println("Received " + image.getHeight() + "x" + image.getWidth() + ": " + System.currentTimeMillis());
ImageIO.write(image, "jpg", new File("C:\\Users\\Jakub\\Pictures\\test2.jpg"));
serverSocket.close();
}
}
再次打印23。
Groovy是否希望我总是有一个临时变量:( ??
答案 0 :(得分:5)
groovy解析器认为你正在做
println (0..9)
然后将其余部分用于println的结果
只需给解析器一个外围的括号
println( (0..9).findAll {
(it % 3 == 0 || it % 5 == 0)
}.sum() )