将所有类代码保存为单独的.java文件是否很重要?
Outer.java,
Inner.java,
Test.java
Run Code Online (Sandbox Code Playgroud)
或者我可以将一个java文件创建为Test.java.请解释匿名类,如何在java中创建匿名类,与普通类相比有哪些优势/劣势?
class Outer {
private int data = 50;
class Inner {
void msg() {
System.out.println("Data is: " + data);
}
}
}
class Test {
public static void main(String args[]) {
Outer obj = new Outer();
Outer.Inner in = obj.new Inner();
in.msg();
}
}
Run Code Online (Sandbox Code Playgroud) @FunctionalInterface
interface MyLambda {
void apply1();
int apply2(int x, int y);
}
Run Code Online (Sandbox Code Playgroud)
现在使用Lambda表达式,为什么Java不能将下面的两个代码分开,因为它清楚地区分了两个代码:
MyLambda ml1 = () -> System.out.println("Hello");
MyLambda ml2 = (x, y) -> x+y;
Run Code Online (Sandbox Code Playgroud) 我对一个看似简单的应用程序有疑问。它应该做什么:
-读出(硬编码)目录的文件(*.jpg)
-使用所述 jpg 包含的元数据(通过实现的库获得)生成目录(./year/month/)
- 将文件复制到相应的目录中。
它没有什么: - 将文件复制到相应的目录中,因为它找不到原始文件(它之前自己读出的)。老实说,我不知道为什么会这样。
这里是源代码:
package fotosorter;
import com.drew.imaging.jpeg.JpegMetadataReader;
import com.drew.imaging.jpeg.JpegProcessingException;
import com.drew.metadata.Metadata;
import com.drew.metadata.exif.ExifIFD0Directory;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Date;
public class Fotosorter {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws JpegProcessingException, IOException {
File startdir = new File(System.getProperty("user.dir"));
FileFilter jpg = new FileFilter() {
@Override
public boolean accept(File pathname) {
return pathname.getAbsoluteFile().toString().toLowerCase().endsWith(".jpg");
}
};
File dir = new File(startdir, "bitmaps"+File.separator+"java-temp"); …Run Code Online (Sandbox Code Playgroud) 我正在做一个有趣的小项目,它本质上是一个小战斗模拟器。我试图在 C++ 中使用一个类似于 struct 的类,就像使用它来创建一个对象一样(在这种情况下,是一个字符或“实体”,因为类被称为)。尝试从主函数调用所述类中的任何整数时,标题中出现错误。
class entity{
public int health;
public int accuracy;
public int power;
public int defense;
}
Run Code Online (Sandbox Code Playgroud)
和
public class Tutorial {
static Random rnd = new Random();
entity player;
player.health = 100; // Issue on the health part
player.accuracy = 19; // Issue on the accuracy part
player.power = 15; // Issue on the power part
player.defense = 18; // I think you get it by now...
Run Code Online (Sandbox Code Playgroud)
我已经环顾了一段时间以找到一些解释,但是我找不到任何解释错误的性质以及针对我的情况的可能修复方法。如果我能得到这些,那就太好了。
运行此程序显示错误的输出.我的文件"values.txt"包含
运行程序后45678的输出.00000
import java.util.Scanner;
public class array{
public static void main(String[] args)throws IOException
{
final int SIZE = 6;
int[] numbers = new int[SIZE];
int index = 0;
File fl = new File("values.txt");
Scanner ab = new Scanner(fl);
while(ab.hasNext() && index < numbers.length)
{
numbers[index] = ab.nextInt();
index++;
System.out.println(numbers[index]);
}
ab.close();
}
}
Run Code Online (Sandbox Code Playgroud) 假设我有一个Animal接口和特定的类来实现它Cat和Dog. 在程序的入口处,有这个:
if (choice == 0) {
Animal A = new Cat();
}
else if (choice == 1) {
Animal A = new Dog();
}
Run Code Online (Sandbox Code Playgroud)
我希望在编译时(例如,在 Maven 构建期间)choice填充参数 ,以便:
这样做的原因是“Cat”和“Dog”类具有单独/不同的依赖关系。
基本上,目标是为“A 是一只猫”制作一个版本,为“A 是一只狗”制作另一个版本。当我打包 JAR 时,运行 JAR 的用例将是“Cat”或“Dog”(取决于环境),而不是“Cat”和“Dog”——所以,不需要所有项目中的依赖项。
实现这一目标的最佳方法是什么?
我刚刚用Homebrew安装了node和npm,他们都工作正常,直到今天我一直遇到npm命令找不到错误.
当运行$ whereis节点时,我什么也得不到
当我执行$哪个节点时,我看到/ usr/local/bin/node
当我做$ node -v时,我看到v4.4.7
当我做$ whereis npm时,我什么也得不回来
当我做npm的时候,我什么也得不回来
当我执行$ npm -v时,我看到-bash:npm:command not found
我试过了
$ brew update
$ brew uninstall npm
$ brew install npm
Run Code Online (Sandbox Code Playgroud)
我还确保设置了我的$NODE_PATH环境变量:
# In ~/.bash_profile file:
export NODE_PATH="/usr/local/lib/node_modules"
Run Code Online (Sandbox Code Playgroud)
我也按照https://himanen.info/solved-npm-command-not-found/中的说明进行操作
似乎没有什么工作,我继续得到npm:当我在任何npm的文件夹中运行任何命令时找不到命令.有任何想法吗?谢谢
我按降序发送结果,但我得到升序输出
List<myEntity> myData = new ArrayList<>();
Map<Integer,List<myEntity>> myid = new LinkedHashMap<>();
try {
myData = myService.getData(id);
myid = myData.stream().collect(Collectors.groupingBy(myEntity::getDataId));
Run Code Online (Sandbox Code Playgroud)
这里 mydata 按 desc 顺序排序,但在按组数据 id 创建集合后,我的列表按升序排序。我希望我的收藏列表是降序而不是升序。
下面的代码有什么区别?
task A {
println 'configuration'
}
task B << {
println 'action'
}
Run Code Online (Sandbox Code Playgroud)
我认为它与评估有关.
即,始终评估任务A,而仅在执行任务B时评估任务B.
当方法不获取记录时,如何配置 Spring Boot 以在 GET 方法(通常是 findAll 方法)中返回 204?我不想在每种方法中都做处理,键入下面的代码:
if(!result)
return new ResponseEntity<Void>(HttpStatus.NO_CONTENT);
return new ResponseEntity<Void>(HttpStatus.OK)
Run Code Online (Sandbox Code Playgroud)
我想改变这个方法:
@GetMapping
public ResponseEntity<?> findAll(){
List<User> result = service.findAll();
return !result.isEmpty() ?
new ResponseEntity<>(result, HttpStatus.OK) : new ResponseEntity<Void>(HttpStatus.NO_CONTENT);
}
Run Code Online (Sandbox Code Playgroud)
在这个:
@GetMapping
public List<User> findAll(){
return service.findAll();
}
Run Code Online (Sandbox Code Playgroud)
如果findAll()的结果为空或 null,那么我的控制器应该返回204而不是200。
java ×8
bash ×1
class ×1
copy ×1
file ×1
gradle ×1
homebrew ×1
java-8 ×1
java-stream ×1
lambda ×1
node.js ×1
npm ×1
npm-install ×1
rest ×1
spring-boot ×1