Questions tagged «java»

Java是一种流行的高级编程语言。如果您在使用或理解语言本身时遇到问题,请使用此标签。这个标签很少单独使用,最常与[spring],[spring-boot],[jakarta-ee],[android],[javafx],[gradle]和[maven]结合使用。

10
为什么应该首选Java类的接口?
PMD将报告以下违规行为: ArrayList<Object> list = new ArrayList<Object>(); 违反是“避免使用类似'ArrayList'的实现类型;而是使用接口”。 以下行将纠正违规: List<Object> list = new ArrayList<Object>(); 为什么要用后者List代替ArrayList?

12
Java中volatile关键字的最简单易懂的示例
我正在阅读有关Java中的volatile关键字的信息,并完全理解了它的理论部分。 但是,我要寻找的是一个很好的案例,它显示了如果变量不是volatile的话会发生什么。 以下代码段未按预期运行(从此处获取): class Test extends Thread { boolean keepRunning = true; public void run() { while (keepRunning) { } System.out.println("Thread terminated."); } public static void main(String[] args) throws InterruptedException { Test t = new Test(); t.start(); Thread.sleep(1000); t.keepRunning = false; System.out.println("keepRunning set to false."); } } 理想情况下,如果keepRunning不是volatile,则线程应无限期继续运行。但是,它确实会在几秒钟后停止。 我有两个基本问题: 谁能用例子解释易失性?不符合JLS的理论。 …


7
为什么flatMap()之后的filter()在Java流中不是“完全”惰性的?
我有以下示例代码: System.out.println( "Result: " + Stream.of(1, 2, 3) .filter(i -> { System.out.println(i); return true; }) .findFirst() .get() ); System.out.println("-----------"); System.out.println( "Result: " + Stream.of(1, 2, 3) .flatMap(i -> Stream.of(i - 1, i, i + 1)) .flatMap(i -> Stream.of(i - 1, i, i + 1)) .filter(i -> { System.out.println(i); return true; …


9
Java等价于PHP的implode(',',array_filter(array()))
我经常在PHP中使用这段代码 $ordine['address'] = implode(', ', array_filter(array($cliente['cap'], $cliente['citta'], $cliente['provincia']))); 它清除空字符串,并用“,”将其连接。如果仅剩一个,它不会添加多余的逗号。它不会在末尾添加逗号。如果没有,则返回空字符串。 因此,我可以获得以下结果之一 "" "Street abc 14" "Street abc 14, 00168" "Street abc 14, 00168, Rome" 在Java中无需添加外部库(针对Android设计)的最佳Java实现(更少的代码)是什么?
75 java  php 

6
从发布请求中读取JAX-RS客户端中的响应正文
在移动应用程序和Web服务之间具有某种代理,发出发布请求时的响应使我们感到困惑。我们收到状态为200的回复:确定。但是我们找不到/提取JSON响应主体。 Client client = ClientBuilder.newClient(); WebTarget webTarget = client.target(WEBSERVICE_BASE_LOCATION + "mobileDevices?operatorCode=KPNSCP"); String jsonString = "{\"osVersion\":\"4.1\",\"apiLevel\":16,\"devicePlatform\":\"ANDROID\"}"; Builder builder = webTarget.request(); Response response = builder.post(Entity.json(jsonString)); 我们正在使用JAX-RS。有人可以提供一些提示来String从服务器响应中提取JSON正文()吗?

16
com.sun.jdi.InvocationException发生了调用方法
我只想创建一个类的对象,但是在调试时出现此错误。有人可以告诉我问题是什么吗?该代码的位置在某些Spring(2.5)Service类中。 还有一个类似的问题:OJB参考描述符1:0关系?我应该将自动检索设置为false吗? 非常感谢〜
75 java  spring 

13
如何在Java中使用ResultSet获取行数?
我正在尝试创建一个简单的方法,该方法接收ResultSet作为参数并返回一个包含ResultSet的行数的int。这是执行此操作的有效方法吗? int size = 0; try { while(rs.next()){ size++; } } catch(Exception ex) { System.out.println("------------------Tablerize.getRowCount-----------------"); System.out.println("Cannot get resultSet row count: " + ex); System.out.println("--------------------------------------------------------"); } 我尝试了这个: int size = 0; try { resultSet.last(); size = resultSet.getRow(); resultSet.beforeFirst(); } catch(Exception ex) { return 0; } return size; 但是我说错了 com.microsoft.sqlserver.jdbc.SQLServerException: The requested …
75 java  jdbc  resultset 

13
无法解决:IntelliJ Idea中带有gradle的com.google.android.gms:play-services
我正在尝试在IntelliJ Idea中将Google Play服务添加到我的libGDX项目中。我在这里遵循了安装指南:https : //developers.google.com/android/guides/setup 看起来很简单。我只是在相应部分的build.gradle中添加了这些行,所以现在看起来像: project(":android") { apply plugin: "android" apply plugin: 'com.android.application' configurations { natives } dependencies { compile project(":core") compile "com.badlogicgames.gdx:gdx-backend-android:$gdxVersion" natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-armeabi" natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-armeabi-v7a" natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-arm64-v8a" natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-x86" natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-x86_64" compile "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion" natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-armeabi" natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-armeabi-v7a" natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-x86" compile "com.badlogicgames.gdx:gdx-freetype:$gdxVersion" natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-armeabi" natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-armeabi-v7a" natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-arm64-v8a" …


6
迭代器与
在一次采访中有人问我,使用迭代器而不是for循环的优点是for什么? 有人可以回答吗?


2
Java流toArray()转换为特定类型的数组
也许这很简单,但实际上我对Java 8功能并不了解,并且不知道如何实现。我有一条包含以下文本的简单行: “密钥,名称” 并且我想将该行转换为String数组,并用逗号(,)分隔每个值,但是,我还想在返回最后一个数组之前修剪每个字段,所以我做了以下工作: Arrays.stream(line.split(",")).map(String::trim).toArray(); 但是,这将返回Object []数组而不是String []数组。经过进一步检查,我可以确认内容实际上是String实例,但是数组本身是Object元素。让我说明一下,这就是调试器对返回对象的说明: Object[]: 0 = (String) "Key" 1 = (String) "Name" 据我所知,问题在于映射调用的返回类型,但是如何使它返回String []数组呢?

6
Spring配置XML模式:有无版本?
我是Spring的新手。令我感到困惑的是,有时我看到带有版本化模式的XML配置文件,但有时却看到非版本化模式的XML配置文件。例如,有时我看到类似 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <context:annotation-config/> <context:component-scan base-package="base.package"/> </beans> 有时像这样: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:annotation-config/> <context:component-scan base-package="base.package"/> </beans> 请注意,两个示例中的spring-beans和spring-context模式是不同的。 所以,我的问题是,您将使用哪种样式,为什么使用?特别是,将来版本更新的架构会不可用,并且当Spring更新架构时,非版本化的架构会与当前应用程序保持兼容吗? 另一个问题是,在哪里可以找到版本化的弹簧模式列表? 非常感谢!
75 java  spring 

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.