我String今天正在使用相对较大的阵列.(大约400 x 400)我想知道如何使一个阵列与另一个阵列完全相同.例如,
String[][] array1 = new String[400][400];
String[][] array2 = array1;
Run Code Online (Sandbox Code Playgroud)
使一个数组等于另一个数组与循环每个元素并使其等于另一个数组中的相应位置相同吗?(如下)
for(int y = 0; y < 400; y++) {
for(int x = 0; x < 400; x++) {
array2[x][y] = array1[x][y];
}
}
Run Code Online (Sandbox Code Playgroud)
现在循环方法与使一个数组等于另一个数组相同吗?或者第一/第二快于另一个?就个人而言,我认为第一个会更快,因为没有递归或者必须array2在递归之前手动分配内存.但是,我不知道从哪里开始寻找这些信息,我想了解Java如何处理这些事情.
我有一个TextBoxWinForm,我想每次有人按下TextBox中的一个键时执行一些代码.我正在查看事件属性菜单,并查看KeyDown事件,但不知道如何向其中添加代码.
System.arraycopy获取ava.lang.ArrayIndexOutOfBoundsException ..我试图将数据从一个数组复制到下一个数组.但我得到一个例外
private String array[] = { "NO DATA YET" };
private void setListData()
{
String array2[] = { "Iphone", "Tutorials", "Gallery", "Android", "item 1", "item 2", "item3", "item 4" };
System.arraycopy(array2, 0, array, 0, array2.length);
}
Run Code Online (Sandbox Code Playgroud) 我发现了一个非常有趣的问题.
当我使用以下代码时:
int main() {
char * in = "hi, ";
char str[10];
strncpy(str, in, 2);
printf("output = %s", str);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我的结果是什么,printf没有用.
但如果我用这个:
int main() {
char * in = "hi, ";
char * str = malloc(sizeof(char) * 10) ;
strncpy(str, in, 2);
printf("output = %s", str);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我能得到我的期望.
为什么会这样?是因为堆栈和堆?究竟是如何产生这种巨大差异的呢?
此代码导致分段错误:
int main(){
char *p;
char a[50] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
p = (char *)malloc(50*sizeof(char));
if(!p){
cout << "Allocation Failure";
cout << "\n";
}
else{
cout << "Allocation Success";
cout << "\n";
p = a;
cout << p;
cout << "\n";
free(p);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
执行该程序后的输出是:
Allocation Success
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
Segmentation fault
Run Code Online (Sandbox Code Playgroud)
我无法找到错误.可能是什么原因?
我对Java很新,我认为这和其他语言一样.
对于字符串:
String line = "3::Daniel::Louis||##2::Leon: the Professional::1994||6::Jean::Reno||7::Gary::Oldman||8::Natalie::Portman||##3::Scarface::1983||9::Al::Pacino||10::Michelle::Pfeiffer";
Run Code Online (Sandbox Code Playgroud)
我想把它分开||##.
但:
for(String s : line.split("||##")) {
System.out.println("|"+s+"|");
}
Run Code Online (Sandbox Code Playgroud)
收益:
||
|3|
|:|
|:|
|D|
|a|
|n|
|i|
Run Code Online (Sandbox Code Playgroud)
...等
我在期待:
3::Daniel::Louis
Leon: the Professional
Run Code Online (Sandbox Code Playgroud)
...等
我究竟做错了什么?
我知道如何将函数作为另一个函数的参数传递.但我不知道传递给pthread的函数的参数是否可以是另一个函数.这甚至可能吗?
以下是编译好的示例代码,但不起作用:
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
pthread_t idThread;
void aFunction(){
while(1){
fprintf(stderr,"I've been called!\n");
usleep(1000000);
}
}
void * threadFunction(void *argFunc){
// Do here some stuff;
// ...
// Now call the function passed as argument
void (*func)() = argFunc;
}
int thread_creator(void(*f)()){
// I want to use as argument for threadFunction the f function
pthread_create(&idThread, NULL, threadFUnction, (void *)f);
}
int main(){
thread_creator(aFunction);
while(1);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 在我搜索的任何地方,它都说你可以通过使用获得环境变量 System.getenv(str).
它不适合我.以下是我正在做的事情:操作系统:Mac OS x 10.7 Java 1.6.x
如果我export abc=/hello/在我的终端然后echo $abc,它给了我变量.如果我关闭终端,再次重新打开它echo $abc,它就消失了.为了解决这个问题,我编辑了我的.bash_profile文件并插入export abc=/hello/.关闭终端,做echo $abc,它的工作原理.所以我明白env变量现在是永久性的.
现在,如果在我的java控制台应用程序中,我打印System.getenv("abc"),它返回null.我错过了什么?
我正在做一个涉及董事会的任务.基本代码是给我们修改的,但是我不明白:for()的参数是什么意思.它是否通过了所有的董事会ArrayList?
private ArrayList<MovingElement> moveElems = new ArrayList<MovingElement>();
for (MovingElement mElement : moveElems) {
mElement.step();
}
Run Code Online (Sandbox Code Playgroud) 我对JavaFX中的事件处理有疑问.根据教程(以及我遇到的其他示例),事件处理在JavaFX中按以下方式进行:
Button addBtn = new Button("Add");
addBtn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
System.out.println("Add Clicked");
}
});
Run Code Online (Sandbox Code Playgroud)
但是,我想知道,如果我可以"处理"按钮,请单击以下方式:
Button addBtn = new Button("Add");
addBtn.setOnAction(new addButtonClicked());
Run Code Online (Sandbox Code Playgroud)
这里addButtonClicked()是我自己的类(与它自己的一套方法和功能),我已经定义并写入到办理按一下按钮的动作.
有没有办法为JavaFX中的按钮附加我自己的事件处理程序类?