Questions tagged «java»

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

9
在ArrayList中移动项目
我一直在玩ArrayLists。我想要实现的是一种执行以下操作的方法: Item 1 Item 2 Item 3 Item 4 我试图能够将列表中的项目向上移动,除非它已经位于顶部,在这种情况下它将保持不变。例如,如果项目3被移动,则列表将为: Item 1 Item 3 Item 2 Item 4 从目前的了解来看,我想要一些类似的东西: IF arrayname index is not equal to 0 THEN move up ELSE do nothing 我苦苦挣扎的部分是“向上移动”部分。如何实现此目标的任何技巧或代码示例都将受到赞赏。
77 java  arraylist 

2
java.lang.RuntimeException:com.android.builder.dexing.DexArchiveMergerException:无法在Android Studio 3.0中合并dex
关闭。这个问题不能重现或由错别字引起。它当前不接受答案。 想改善这个问题吗?更新问题,使其成为Stack Overflow的主题。 3年前关闭。 改善这个问题 这是我的应用程序级别gradle文件: apply plugin: 'com.android.application' android { compileSdkVersion 26 buildToolsVersion '27.0.0' useLibrary 'org.apache.http.legacy' defaultConfig { multiDexEnabled true applicationId 'com.example.test' minSdkVersion 16 targetSdkVersion 26 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } productFlavors { } dexOptions { incremental true } } …
77 java  android  gradle 

14
Gradle-无法找到或加载主类
我正在尝试使用Gradle运行一个非常简单的项目,并在使用时遇到以下错误gradlew run command: could not find or load main class 'hello.HelloWorld' 这是我的文件结构: SpringTest -src -hello -HelloWorld.java -Greeter.java -build -libs -tmp -gradle -wrapper -build.gradle -gradlew -gradlew.bat 我排除了libs和tmp文件夹的内容,因为我认为这与该问题无关,但是我可以在需要时添加它。 这是我的build.gradle文件: apply plugin: 'java' apply plugin: 'application' apply plugin: 'eclipse' mainClassName = 'hello/HelloWorld' repositories { mavenLocal() mavenCentral() } dependencies { compile "joda-time:joda-time:2.2" } jar …


6
多个方法参数上的@Cacheable键
从spring文档中: @Cacheable(value="bookCache", key="isbn") public Book findBook(ISBN isbn, boolean checkWarehouse, boolean includeUsed) 如何指定@Cachable使用isbn和checkWarehouse作为密钥?
77 java  spring  ehcache 

4
我应该如何命名java.util.Map?[关闭]
已关闭。这个问题是基于观点的。它当前不接受答案。 想改善这个问题吗?更新问题,以便通过编辑此帖子以事实和引用的形式回答。 1年前关闭。 改善这个问题 我有一个java.util.Map从逻辑名称映射到与该名称一起使用的参数集的映射。 Map<String,Parameters> howShouldINameThee = ...; 该地图的最佳名字是什么? 我应该去简单,只需要调用这个parameters还是parametersMap? 我是否在名称中包含有关密钥的信息,paramtersByName以便如何String更明显地使用密钥?

4
Java8将java.util.Date转换为java.time.ZonedDateTime
我得到以下异常而试图转换java.util.Date到java.time.LocalDate。 java.time.DateTimeException: Unable to obtain ZonedDateTime from TemporalAccessor: 2014-08-19T05:28:16.768Z of type java.time.Instant 代码如下: public static Date getNearestQuarterStartDate(Date calculateFromDate){ int[] quaterStartMonths={1,4,7,10}; Date startDate=null; ZonedDateTime d=ZonedDateTime.from(calculateFromDate.toInstant()); int frmDateMonth=d.getMonth().getValue(); 我ZonedDateTime上课的方式有问题吗? 根据文档,这应将java.util.Date对象转换为ZonedDateTime。上面的日期格式是标准日期? 我必须在Joda时间回退吗? 如果有人可以提供一些建议,那就太好了。


1
Collectors.toSet()和HashSet
采取以下示例代码行: Set<String> someSet = someColletion.stream().map(p -> p.toString()).collect(Collectors.toSet()); 我想要一个HashSet。将调试器用于代码,我确实得到了HashSet。我看了看java.util.stream.Collectors.toSet()下面的代码: public static <T> Collector<T, ?, Set<T>> toSet() { return new CollectorImpl<>((Supplier<Set<T>>) HashSet::new, Set::add, (left, right) -> { left.addAll(right); return left; }, CH_UNORDERED_ID); } 合同担保a Set,实施决定a HashSet; 似乎是合理的。但是,我的实现需要aHashSet而不是仅任何old保证的恒定时间查找Set。如果的实现toSet()决定使用FooSet完全在其权限范围内的a ,则我的实现受到了损害。 解决此问题的最佳实践是什么?

10
模拟Java InputStream
请提供指针以帮助我模拟该java InputStream对象。这是我希望模拟的代码行: InputStreamReader inputData = new InputStreamReader(System.in); bufferdReader = new BufferedReader(inputData); bufferdReader.readLine();
77 java  mocking 

5
是否有一个标准的Java异常类表示“找不到对象”?
考虑以下一般形式的函数: Foo findFoo(Collection<Foo> foos, otherarguments) throws ObjectNotFoundException { for(Foo foo : foos){ if(/* foo meets some condition*/){ return foo; } } throw new ObjectNotFoundException(); } 例如,一个具体案例是: User findUserByName(Collection<User> users, String name) throws ObjectNotFoundException { for(User user : users){ if(user.getName().equals(name)){ return user; } } throw new ObjectNotFoundException(); } 如果找不到对象,这些函数将引发异常。我可以为此创建一个自定义的异常类(在示例中ObjectNotFoundException),但是我更喜欢使用现有的类。但是,我在标准java库中找不到具有此含义的任何异常类。您知道这里是否可以使用标准例外吗?

18
在Spring Boot上删除“使用默认安全密码”
我在Spring Boot的应用程序中添加了一个自定义安全配置,但是有关“使用默认安全密码”的消息仍在LOG文件中。 有什么要删除的吗?我不需要此默认密码。看来Spring Boot无法识别我的安全策略。 @Configuration @EnableWebSecurity public class CustomSecurityConfig extends WebSecurityConfigurerAdapter { private final String uri = "/custom/*"; @Override public void configure(final HttpSecurity http) throws Exception { http.csrf().disable(); http.headers().httpStrictTransportSecurity().disable(); http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS); // Authorize sub-folders permissions http.antMatcher(uri).authorizeRequests().anyRequest().permitAll(); } }

15
使用数组作为switch中的case语句
我试图做这样的事情,即在switch语句中使用数组。Java可能吗?如果不是,请说明可能的解决方案。 boolean[] values = new boolean[4]; values[0] = true; values[1] = false; values[2] = false; values[3] = true; switch (values) { case [true, false, true, false]: break; case [false, false, true, false]: break; default: break; }

10
访问限制:由于对所需库.. \ jre \ lib \ rt.jar的限制而无法访问
我正在尝试修改一些旧版代码,而后得到以下错误: 访问限制:由于必需的库.. \ jre \ lib \ rt.jar的限制,无法访问标头类型的方法create(JAXBRIContext,Object) 对于这些导入语句: import com.sun.xml.internal.bind.api.JAXBRIContext; import com.sun.xml.internal.ws.api.message.Header; import com.sun.xml.internal.ws.api.message.Headers; import com.sun.xml.internal.ws.developer.WSBindingProvider; 一直在搜索这可能意味着什么以及如何解决它,但是未能找到明确的答案。有些帖子似乎暗示我包含一些实现类的类JAR,这些类现在可以作为核心Java发行版的一部分使用,但是据我所见,我所包含的所有JAR类都不包含上述类的不同/旧版本。 任何人都可以告诉我该错误的全部原因以及如何解决此问题? 感谢您的事先帮助, 奥利


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.