我实际上认为我很清楚Java中的值传递是如何工作的,因为那是我通过的SCJP证书的一部分.直到今天,当我在工作时发现了这样的方法:
public void toCommand(Stringbuffer buf) {
buf.append("blablabla");
}
Run Code Online (Sandbox Code Playgroud)
然后该方法的调用者使用如下函数:
StringBuffer buf = new StringBuffer();
toCommand(buf);
String str = buf.toString();
Run Code Online (Sandbox Code Playgroud)
现在我认为该代码会给str值"",但实际上它给了它来自mehod的值.这怎么可能?我觉得Java中没有像那样的东西?
无论哪种方式......在Java中编写这样的代码应该被视为一种不好的做法,对吧?因为我可以想象它会带来一些混乱.
我实际上花了一些时间来搜索这个,但我对这些消息来源的解释是,它不应该工作.我错过了什么?
http://www.yoda.arachsys.com/java/passing.html
http://javadude.com/articles/passbyvalue.htm
Run Code Online (Sandbox Code Playgroud)
塞巴斯蒂安
有没有办法只使用正则表达式进行整数增量替换.
这是问题所在,我的文本文件包含1 000 000行所有以%开头
我想使用正则表达式逐步替换整数#.
input:
% line one
% line two
% line three
...
output:
1 line one
2 line two
3 line three
...
Run Code Online (Sandbox Code Playgroud) 我有这个功能:
/**
* Helper function that adds the values of b to the elements of a, treating
* all keys that exist in b but not in a, as existent in a with value 0. NB:
* It operates IN PLACE.
* @param a The {@link java.util.Map} which will hold the result
* @param b The {@link java.util.Map} which will be added to a
*/
private static void sumMaps(Map<?, Integer> a, Map<?,Integer> b)
{
for (Object key : b.keySet()) { …Run Code Online (Sandbox Code Playgroud) 我试图在if - else语句中强制转换对象,如下所示:
if(sourceSystem.equalsIgnoreCase("Src")) {
MO object = (MO) transformer.create(message,sourceSystem,flowName);
} else {
UO object = (URO) transformer.create(message,sourceSystem,flowName);
}
Run Code Online (Sandbox Code Playgroud)
但是它不能在这些之外访问?
validator.validate(object);
Run Code Online (Sandbox Code Playgroud)
无法解析为变量.但肯定是在if-else语句中创建了一个赋值对象.将始终创建对象,因此编译器为什么告诉我它无法解析为变量.是的,如果我需要访问UO和MO之间的常用方法,我会获得本地和全球的可用物品.
我在类中设置了一个方法来设置"位置X"和"位置Y".
public class Player {
int positionX, postinionY;
public void setPosition (int position_X, int position_Y)
{
positionX = position_X;
postinionY = position_Y;
}
}
Run Code Online (Sandbox Code Playgroud)
我有另一个类"世界",其中有一个数组,称为网格.我必须使用Player中位置x和y的值来从网格中获取位置.
public class World extends Player {
int [] [] grid = new int [16] [16];
public int getLocationID (int x, int y)
{
return grid [x][y];
}
}
Run Code Online (Sandbox Code Playgroud)
我需要帮助来获取位置x,y(玩家)并使用它们代替x,y(世界)
(对主持人)请注意我之前发布了一个相关问题,但这是一个更完整的帖子所以请不要将其作为重复关闭.你可以关闭上一篇文章.
每当我在控制台输出中得到-1时,在输出流中没有写入数据,每当我在控制台输出中得到3时,有效数据在输出流中被写入.-1和3的出现在不同的场合是随机的.
这是代码
public void decrypt(InputStream in, OutputStream out) {
try {
// Bytes read from in will be decrypted
in = new CipherInputStream(in, dcipher);
// Read in the decrypted bytes and write the cleartext to out
int numRead = 0;
System.out.println(in.read(buf));
while ((numRead = in.read(buf)) >= 0) {
out.write(buf, 0, numRead);
}
//out.close();
}
catch (java.io.IOException e) {
}
}
Run Code Online (Sandbox Code Playgroud)
这是控制台输出
the Cell Content : ¼OKs>N?h¸GX&ŸH
-1
the Cell Content : 3Ëù%¥þ-]'±ŠM݆
3
the Cell Content : ´`?û…óï>»†µýÆ$
-1 …Run Code Online (Sandbox Code Playgroud) 我试图在我的JFrame上显示两张图片,我发现的方式是使用图标和JLabel,这看起来很简单,我没有遇到这个问题.但是当谈到定位图像时,我无法让它工作.我在Linux机器上因此是转发式的.我在我的项目中创建了一个名为pics的文件夹,名为399assig1.
ImageIcon icon1 = createImageIcon("/home/dsk03/ugrad/jeanbern/workspace/C291/workspace/399assig1/pics/fur-05.jpg","First");
this.label1 = new JLabel("Picture 1", icon1, JLabel.CENTER);
ImageIcon icon2 = createImageIcon("pics/fur.png","Second");
this.label2 = new JLabel("Picture 2", icon2, JLabel.CENTER);
Run Code Online (Sandbox Code Playgroud)
这是我得到的错误
Couldn't find file: /home/dsk03/ugrad/jeanbern/workspace/C291/workspace/399assig1/pics/fur-05.jpg
Couldn't find file: pics/fur.png
Run Code Online (Sandbox Code Playgroud) 我正在使用类似的东西在DOS命令窗口中执行java应用程序
java -cp abcclient.jar;junit-4.4.jar;myapp.jar MyMainClass
Run Code Online (Sandbox Code Playgroud)
我需要引用在我的应用程序文件夹之外的特定文件夹中找到的许多其他jar.无论如何,我可以在上面的命令行中声明文件夹名称,让java从该文件夹中引用必要的jar.
谢谢
我正在开发一个应用程序,其中订阅者发送SMS以获取任何包.
在我的程序中,当我收到用户请求时,我必须创建Subscriber对象.要初始化订户对象,我必须查询后端数据库和IN节点,然后设置订户对象的属性.
请为此建议,我应该使用Factory模式还是Builder?或任何好的选择?有什么建议我将代码用于查询数据库并在Factory类中调用XMLRPC?
我的课看起来像以下
public class Subscriber {
private String subno;
private String subPackageType;
private String subTariff;
private String subRequest;
private boolean isTransferable;
}
Run Code Online (Sandbox Code Playgroud)
现在subno,subRequest我从sms获得,subPackageType来自数据库,subTariff来自IN节点,发送XMLRPC命令.
我也有更多的属性,但我只提一些提供概念.
问候,
imran
我在下面的代码中遇到此问题,因为它不会运行但会生成nullpointer异常.我在这里尝试做的是获取输入一个字符串数组然后吐出它但逗号然后将其传递给Integer类型然后将其存储在向量中.然后从该数组中获取最大数量.代码显示没有错误,但很难找到什么错误.
import java.util.Collections;
import java.util.Vector;
public class Splitting {
/**
* @param
*/
protected int[] temp;
Vector<Integer> vec = new Vector<Integer>();
public void split(String input) {
if (input == null) {
String[] str;
str = input.split(",");
temp = new int[str.length];
for (int i = 0; i < str.length; i++) {
temp[i] = Integer.parseInt(str[i]);
vec.add(temp[i]);
}
}
System.out.println(vec);
Collections.sort(vec);
System.out.println(vec);
Collections.max(vec);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Splitting obj = new Splitting();
obj.split("12,65,21,23,89,67,12");
}
}
Run Code Online (Sandbox Code Playgroud)