Questions tagged «java»

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

9
Java:子包可见性?
我的项目中有两个软件包:odp.proj和odp.proj.test。我希望某些方法仅对这两个软件包中的类可见。我怎样才能做到这一点? 编辑:如果在Java中没有子包的概念,有没有解决的办法?我有某些方法只想供测试人员和该软件包的其他成员使用。我应该把所有东西都放进同一个包装吗?使用广泛的反思?

10
将Java数组转换为可迭代
我有一个原始数组,例如int,int [] foo。它可能是小型的,也可能不是。 int foo[] = {1,2,3,4,5,6,7,8,9,0}; 从中创建一个的最佳方法是Iterable<Integer>什么? Iterable<Integer> fooBar = convert(foo); 笔记: 请不要回答使用循环(除非您可以对编译器如何对它们做一些聪明的事情给出很好的解释?) 另请注意 int a[] = {1,2,3}; List<Integer> l = Arrays.asList(a); 甚至不会编译 Type mismatch: cannot convert from List<int[]> to List<Integer> 还要检查 为什么数组不能分配给Iterable? 在回答之前。 另外,如果您使用某些库(例如Guava),请解释为什么这是最好的。(因为来自Google的答案不是完整的:P) 最后,由于似乎有作业,因此请避免发布作业代码。
150 java  arrays  iterable 


11
使用Java计算两个日期之间的天数
我想要一个Java程序来计算两个日期之间的天数。 键入第一个日期(德语表示法;带空格:“ dd mm yyyy”) 输入第二个日期。 该程序应计算两个日期之间的天数。 如何包含leap年和夏季? 我的代码: import java.util.Calendar; import java.util.Date; import java.util.Scanner; public class NewDateDifference { public static void main(String[] args) { System.out.print("Insert first date: "); Scanner s = new Scanner(System.in); String[] eingabe1 = new String[3]; while (s.hasNext()) { int i = 0; insert1[i] = s.next(); if …

12
说明使用位向量确定所有字符是否唯一
我对位向量如何实现此功能感到困惑(对位向量不太熟悉)。这是给出的代码。有人可以帮我解决这个问题吗? public static boolean isUniqueChars(String str) { int checker = 0; for (int i = 0; i < str.length(); ++i) { int val = str.charAt(i) - 'a'; if ((checker & (1 << val)) > 0) return false; checker |= (1 << val); } return true; } 特别是,这是checker怎么做的?

15
Maven:将自定义外部JAR链接到我的项目的最佳方法是什么?
这是我学习Maven的前两天,但我仍在努力学习基础知识。我有一个外部.jar文件(在公共存储库中不可用),我需要在我的项目中引用该文件,我正在尝试找出我的最佳选择。 这是一个小型项目,没有用于库的中央存储库,因此它必须是本地存储库(以某种方式添加到源代码控制中,不知道它是否应该以这种方式工作?)或.jar需要存储在磁盘在任何正式存储库之外。 1)鉴于我希望项目和库都在源代码控制中,因此使用maven将.jar文件添加到项目引用中的最佳选择是什么? 2)我似乎仍然无法让Eclipse看到依赖性。我手动将其添加到pom的这一部分,它在m2eclipse的Dependencies列表中显示得很好。mvn编译和mvn软件包都成功,但是运行程序会导致: Exception in thread "main" java.lang.Error: Unresolved compilation problems: LibraryStuff cannot be resolved to a type 这是将POM编辑为: <dependency> <groupId>stuff</groupId> <artifactId>library</artifactId> <version>1.0</version> <systemPath>${lib.location}/MyLibrary.jar</systemPath> <scope>system</scope> </dependency> 我是否应该执行mvn install:install-file甚至认为我已经按照上面的方法编辑了pom.xml? 谢谢!
150 java  maven-2  maven  m2eclipse 


16
如何在Java中加密字符串
我需要加密的字符串会显示在2D条码中(PDF-417),所以当有人知道扫描的想法时,它就不会可读。 其他需求: 应该不复杂 它不应包含RSA,PKI基础结构,密钥对等。 它必须足够简单,以摆脱被监视的人,并且必须易于对其他有兴趣获取该数据的公司解密。他们打电话给我们,我们告诉他们标准,或者给他们一些简单的密钥,然后可以将其用于解密。 那些公司可能会使用不同的技术,因此最好坚持不依赖于某些特殊平台或技术的某些标准。 你有什么建议?有一些Java类做encrypt()和decrypt()没有太多的并发症,实现高安全标准?
150 java  encryption 

3
从字符串中删除前3个字符[关闭]
关闭。此问题不符合堆栈溢出准则。它当前不接受答案。 想改善这个问题吗?更新问题,使其成为Stack Overflow 的主题。 6年前关闭。 改善这个问题 删除字符串的前三个字符的最有效方法是什么? 例如: “苹果”更改为“乐” “一只猫”改为“在” 'ab c'更改为'bc'
150 java  string 

2
在Mockito中检测到未完成的存根
运行测试时出现以下异常。我正在使用Mockito进行嘲笑。Mockito库提到的提示无济于事。 org.mockito.exceptions.misusing.UnfinishedStubbingException: Unfinished stubbing detected here: -> at com.a.b.DomainTestFactory.myTest(DomainTestFactory.java:355) E.g. thenReturn() may be missing. Examples of correct stubbing: when(mock.isOk()).thenReturn(true); when(mock.isOk()).thenThrow(exception); doThrow(exception).when(mock).someVoidMethod(); Hints: 1. missing thenReturn() 2. you are trying to stub a final method, you naughty developer! at a.b.DomainTestFactory.myTest(DomainTestFactory.java:276) .......... 来自的测试代码DomainTestFactory。当我运行以下测试时,我看到了异常。 @Test public myTest(){ MyMainModel mainModel = Mockito.mock(MyMainModel.class); Mockito.when(mainModel.getList()).thenReturn(getSomeList()); // …
150 java  mocking  mockito 

13
如何将对象序列化为字符串
我能够将一个对象序列化为一个文件,然后再次还原它,如下面的代码片段所示。我想将对象序列化为字符串并存储到数据库中。谁能帮我? LinkedList<Diff_match_patch.Patch> patches = // whatever... FileOutputStream fileStream = new FileOutputStream("foo.ser"); ObjectOutputStream os = new ObjectOutputStream(fileStream); os.writeObject(patches1); os.close(); FileInputStream fileInputStream = new FileInputStream("foo.ser"); ObjectInputStream oInputStream = new ObjectInputStream(fileInputStream); Object one = oInputStream.readObject(); LinkedList<Diff_match_patch.Patch> patches3 = (LinkedList<Diff_match_patch.Patch>) one; os.close();

9
解析LocalDateTime(Java 8)时,无法从TemporalAccessor获取LocalDateTime
我只是想将Java 8中的日期字符串转换为DateTime对象。运行以下行时: DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd"); LocalDateTime dt = LocalDateTime.parse("20140218", formatter); 我收到以下错误: Exception in thread "main" java.time.format.DateTimeParseException: Text '20140218' could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor: {},ISO resolved to 2014-02-18 of type java.time.format.Parsed at java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:1918) at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1853) at java.time.LocalDateTime.parse(LocalDateTime.java:492) 语法与此处建议的语法相同,但有例外。我正在使用JDK-8u25。

4
如何设置JLabel的背景颜色?
在我的中JPanel,我将的背景设置JLabel为其他颜色。我可以看到单词“ Test”,它是蓝色的,但是背景完全没有变化。如何显示? this.setBackground(Color.white); JLabel label = new JLabel("Test"); label.setForeground(Color.blue); label.setBackground(Color.lightGray); this.add(label);
149 java  swing  jlabel 

3
将InputStream转换为BufferedReader
我正在尝试使用InputStream从Android的Assets目录中逐行读取文本文件。 我想将InputStream转换为BufferedReader以便能够使用readLine()。 我有以下代码: InputStream is; is = myContext.getAssets().open ("file.txt"); BufferedReader br = new BufferedReader (is); 第三行删除以下错误: 这行有多个标记 构造函数BufferedReader(InputStream)未定义。 我想要在C ++中做的事情是这样的: StreamReader file; file = File.OpenText ("file.txt"); line = file.ReadLine(); line = file.ReadLine(); ... 我在做什么错或应该怎么做?谢谢!


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.