Questions tagged «java»

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

3
为什么这个随机值的分布是25/75,而不是50/50?
编辑:所以基本上我想写的是1位哈希double。 我想将映射double到true或false有50/50的机会。为此,我编写了选择一些随机数的代码(仅作为示例,我想在具有规则性的数据上使用它并仍然获得50/50的结果),检查它们的最后一位,并递增y其是否为1,或者n是否为1。0。 但是,此代码始终导致25%y和75%的结果n。为什么不是50/50?为什么会有这样奇怪但直接的(1/3)分布呢? public class DoubleToBoolean { @Test public void test() { int y = 0; int n = 0; Random r = new Random(); for (int i = 0; i < 1000000; i++) { double randomValue = r.nextDouble(); long lastBit = Double.doubleToLongBits(randomValue) & 1; if (lastBit == 1) { …

5
Java&=运算符是否适用&或&&?
假设 boolean a = false; 我想知道是否在做: a &= b; 相当于 a = a && b; //logical AND, a is false hence b is not evaluated. 或另一方面 a = a & b; //Bitwise AND. Both a and b are evaluated.
139 java  operators 


3
为什么不推荐使用Cloneable?
众所周知,CloneableJava 中的接口已损坏。造成这种情况的原因很多,我将不再赘述。其他人已经做到了。这也是Java架构师本身的立场。 因此,我的问题是:为什么还不被弃用?如果核心Java团队已确定它已损坏,那么他们还必须考虑过时。他们反对这样做的原因是什么(在Java 8中仍然不推荐使用)?


8
Java思考:如何获取变量的名称?
使用Java Reflection,是否可以获得本地变量的名称?例如,如果我有这个: Foo b = new Foo(); Foo a = new Foo(); Foo r = new Foo(); 是否有可能实现一种可以找到这些变量名称的方法,如下所示: public void baz(Foo... foos) { for (Foo foo: foos) { // Print the name of each foo - b, a, and r System.out.println(***); } } 编辑:这个问题不同于Java中是否有办法找到传递给函数的变量的名称?因为它更纯粹地问一个问题,即一个人是否可以使用反射来确定局部变量的名称,而另一个问题(包括公认的答案)则更侧重于测试变量的值。
139 java  reflection 

6
将字符串写入输出流
我有几个正在实现OutputStream的输出侦听器。它可以是PrintStream写入stdout或File,也可以写入内存或任何其他输出目标。因此,我在方法中将OutputStream指定为参数。 现在,我已经收到了字符串。在此处写入流的最佳方法是什么? 我应该只使用Writer.write(message.getBytes())吗?我可以给它提供字节,但是如果目标流是字符流,它将自动转换吗? 我是否需要在这里使用一些桥接流?
139 java  stream 

11
RecyclerView-在特定位置获取视图
我有一个与RecyclerView和一起进行的活动ImageView。我正在使用RecyclerView来水平显示图像列表。当我点击在图像上RecyclerView的ImageView在活动中应显示图像的大局观。到目前为止,一切正常。 现在ImageButtons,活动中还有两个:imageButton_left和imageButton_right。当我单击时imageButton_left,中的图像ImageView应左移,并且中的缩略图也RecyclerView应反映此更改。与的情况类似imageButton_right。 我能够旋转ImageView。但是,如何旋转缩略图RecyclerView?我如何获得ViewHolder的ImageView? 码: 活动XML: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <android.support.v7.widget.RecyclerView android:id="@+id/recyclerview" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="10dp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="10dp" android:orientation="vertical"> <ImageView android:id="@+id/original_image" android:layout_width="200dp" android:layout_height="200dp" android:scaleType="fitXY" android:src="@drawable/image_not_available_2" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:gravity="center_horizontal" android:orientation="horizontal"> <ImageButton android:id="@+id/imageButton_left" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="20dp" android:background="@drawable/rotate_left_icon" /> <ImageButton android:id="@+id/imageButton_right" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/rotate_right_icon" …

30
java.lang.ClassNotFoundException:在路径dexpathlist上找不到类
我目前正在一个项目中,我必须使用纯本地ndk。当我尝试从Irrlicht引擎源运行helloworld示例时,它起作用了。然后,按照该示例的相同格式尝试在我的项目中使用它。但是我得到了: 03-14 01:40:05.308: E/AndroidRuntime(799): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.irrlicht.example1/android.app.POMActivity}: java.lang.ClassNotFoundException: Didn't find class "android.app.POMActivity" on path: DexPathList[[zip file "/data/app/com.irrlicht.example1-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.irrlicht.example1-2, /system/lib]] 在运行我的项目时。 这是我的main.cpp文件: #include <android/log.h> #include <jni.h> #include <android_native_app_glue.h> #include "android_tools.h" #ifdef _IRR_ANDROID_PLATFORM_ void android_main(android_app* app) { __android_log_print(4 , "pom" , "nothing"); } #万一 在Android.mk中: LOCAL_PATH := $(call my-dir)/.. IRRLICHT_PROJECT_PATH …
139 java  android  c++  android-ndk 


28
为什么不在“捕获”或“最终”范围内的“尝试”中声明变量?
在C#和Java(可能还有其他语言)中,在“ try”块中声明的变量不在相应的“ catch”或“ finally”块中。例如,以下代码无法编译: try { String s = "test"; // (more code...) } catch { Console.Out.WriteLine(s); //Java fans: think "System.out.println" here instead } 在此代码中,在catch块中对s的引用发生编译时错误,因为s仅在try块的范围内。(在Java中,编译错误是“无法解决”;在C#中,它是“名称s在当前上下文中不存在”。) 解决此问题的一般方法似乎是在try块之前而不是在try块内声明变量: String s; try { s = "test"; // (more code...) } catch { Console.Out.WriteLine(s); //Java fans: think "System.out.println" here instead } 但是,至少对我来说,(1)感觉像一个笨拙的解决方案,(2)它导致变量的范围比程序员预期的范围大(方法的整个其余部分,而不仅仅是在上下文中)。最终尝试捕获)。 我的问题是,此语言设计决定(使用Java,C#和/或任何其他适用的语言)背后的基本原理是什么?


6
<T>(尖括号)在Java中是什么意思?
我目前正在学习Java,最近被尖括号(&lt;&gt;)所困扰。它们到底是什么意思? public class Pool&lt;T&gt;{ public interface PoolFactory&lt;T&gt;{ public T createObject(); } this.freeObjects = new ArrayList&lt;T&gt;(maxsize) } 什么&lt;T&gt;意思 这是否意味着我可以创建类型的对象T?
139 java  generics 

8
Java中变量的内存地址
请看下面的图片。当我们在Java中用new关键字创建对象时,我们正在从OS获取内存地址。 编写时,out.println(objName)我们可以看到一个“特殊”字符串作为输出。我的问题是: 这是什么输出? 如果是操作系统给我们的内存地址: a)如何将该字符串转换为二进制? b)如何获得一个整数变量地址?

10
我如何从jar文件中获取资源“文件夹”?
我在项目的根目录中有一个资源文件夹/程序包,我“不想”加载某个文件。如果我想加载某个文件,我将使用class.getResourceAsStream,我会很好的!我实际上想要做的是在资源文件夹中加载一个“文件夹”,循环访问该文件夹中的文件,并获取每个文件的流,然后读取内容...假定在运行时之前未确定文件名... 我该怎么办?有没有一种方法可以获取jar文件中“文件夹”中文件的列表?请注意,带有资源的Jar文件与从中运行代码的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.