为什么他们决定在Java和.NET(以及其他一些语言)中使字符串不可变?他们为什么不让它变得可变?
以下是两张PNG图片:
在视觉上它们完全相同 - 唯一的区别是在某些像素中有一个半透明背景(您可以下载图像进行检查).
但是当我在JavaFX节点上使用这些图像作为图像光标时,我得到以下结果:
第一个光标(没有部分透明像素)仍然很清晰,但第二个光标变形.
在与问题斗争了一段时间之后,我发现了解决这种差异的算法 - 混合模式:
"预期"方式(例如,您可以在此浏览器中看到)是获取每个通道的值之和,按Alpha值加权:(1 - alpha) * background_color + alpha * foreground_color
.
"JavaFX Cursor"给出了不同的公式:( (1 - alpha) * background_color + alpha^2 * foreground_color
注意方形).
我发现了失真,但我无法弄清楚我做错了什么以及如何纠正这个问题.
这是我测试程序的完整可运行源代码:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.scene.ImageCursor;
import javafx.scene.image.Image;
public class HelloWorld extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
System.out.println(ImageCursor.getBestSize(32, 32));
primaryStage.setTitle("Hello World!");
StackPane root = new StackPane();
root.setCursor(new ImageCursor(new Image("/test-cursor.png"), …
Run Code Online (Sandbox Code Playgroud) 我想知道是否有更好的方法为这个并传递Array
给该方法或每次我想检查一个数字是否在其中时写出来array
.
例如:
public static boolean inArray(int[] array, int check) {
for (int i = 0; i < array.length; i++) {
if (array[i] == check)
return true;
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
我在这里先向您的帮助表示感谢!
是否有一些模板或东西来实现iterface方法访问包装成员?
例如,假设我有
public class MyClass implements List<Something> {
private final List<Something> core;
...
}
Run Code Online (Sandbox Code Playgroud)
现在我想List<Something>
通过将调用传递给包装来实现
@Override
public int size() {
return core.size();
}
Run Code Online (Sandbox Code Playgroud)
等等.
我正在尝试构造一个文件路径以读取XSLT文件,如下所示:
string path = "../_xslt/example.xslt";
StreamReader reader = new StreamReader(path);
Run Code Online (Sandbox Code Playgroud)
...我在控制器(/Controllers/ExampleController.cs)中,'/ _xslt /'文件夹与'/ Controllers'处于同一级别
但是,我得到的错误是:
(System.IO.DirectoryNotFoundException)找不到路径'c:\ windows\system32\_xslt\example.xslt'的一部分.
我是以错误的方式来做这件事的吗?
谢谢你的帮助!
我创建了以下类层次结构:
open class A {
init {
f()
}
open fun f() {
println("In A f")
}
}
class B : A() {
var x: Int = 33
init {
println("x: " + x)
}
override fun f() {
x = 1
println("x in f: "+ x)
}
init {
println("x2: " + x)
}
}
fun main() {
println("Hello World!!")
val b = B()
println("in main x : " + b.x)
}
Run Code Online (Sandbox Code Playgroud)
这段代码的输出是
open class A {
init { …
Run Code Online (Sandbox Code Playgroud) 我写了一个单例类来获取数据库连接.
现在我的问题是:假设有100个用户访问该应用程序.如果一个用户关闭了连接,对于其他99个用户,是否会关闭连接?
这是我的示例程序,它使用单例类来获取数据库连接:
public class GetConnection {
private GetConnection() { }
public Connection getConnection() {
Context ctx = new InitialContext();
DataSource ds = ctx.lookup("jndifordbconc");
Connection con = ds.getConnection();
return con;
}
public static GetConnection getInstancetoGetConnection () {
// which gives GetConnection class instance to call getConnection() on this .
}
}
Run Code Online (Sandbox Code Playgroud)
请指导我.
我正在用Java学习MongoDB.我正在尝试使用Java驱动程序将数据插入MongoDB.我正在插入像MongoDB教程一样,每件事都很好.但是,如果我想插入一个变量,当我运行代码时,驱动程序会抛出这样的错误:
org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for class io.github.ilkgunel.mongodb.Pojo.
Run Code Online (Sandbox Code Playgroud)
我在Stack Overflow中解决了这样的问题,但我无法理解任何事情,我找不到任何解决此错误的方法.我的代码如下.怎么能解决这个问题?
我正在使用此代码:
package io.github.ilkgunel.mongodb;
import org.bson.Document;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Locale;
public class MongoDBBasicUsage {
public static void main(String[] args) {
MongoClient mongoClient;
try {
Pojo pojo = new Pojo();
mongoClient = new MongoClient("localhost", 27017);
MongoDatabase database = mongoClient.getDatabase("MongoDB");
pojo.setId("1");
pojo.setName("ilkay");
pojo.setSurname("günel");
Document document = new Document();
document.put("person", pojo);
database.getCollection("Records").insertOne(document);
} catch (Exception e) {
System.err.println("Bir Hata …
Run Code Online (Sandbox Code Playgroud) 是否有一个更简洁/标准的习惯用法(例如,JDK方法)用于"管道"输入到Java中的输出而不是以下?
public void pipe(Reader in, Writer out) {
CharBuffer buf = CharBuffer.allocate(DEFAULT_BUFFER_SIZE);
while (in.read(buf) >= 0 ) {
out.append(buf.flip());
buf.clear();
}
}
Run Code Online (Sandbox Code Playgroud)
[编辑]请注意,Reader
和Writer
被给予.正确答案将演示如何获取in
和out
形成管道(最好不超过1或2个方法调用).我将接受答案,其中in
和out
是InputStream
和OutputStream
(最好是从/到Reader
/ 的转换Writer
).我不会接受的答案无论是地方in
还是out
是一个子类的Reader
/ InputStream
或Writer
/ OutputStrem
.
java ×8
.net ×1
arrays ×1
bigdecimal ×1
c# ×1
class ×1
code-assist ×1
decimal ×1
eclipse ×1
io ×1
javafx ×1
jdbc ×1
kotlin ×1
mongodb ×1
mutable ×1
overriding ×1
pipe ×1
polymorphism ×1
string ×1
wrapper ×1