我有一些像这样的代码:
public class A {
private final Map<String, Runnable> map = new HashMap<>();
public A() {
map.put("a", () -> a());
map.put("b", () -> b());
}
public int a() {
return 1;
}
public int b() {
return 2;
}
public int c(String s) {
// map.get(s).run(); <= returns void, but
// I need the result of the
// function paired to the string.
// What TODO?
}
}
Run Code Online (Sandbox Code Playgroud)
我没有 - 函数(a(),b())作为地图的值,与字符串配对.我需要运行函数并获取函数的结果,并在函数中返回它c().该run()函数返回void …
例如,在使用时读取文件时fscanf(),似乎记得最后一次终止的位置,而不是再次从文件的开头开始.有人可以详细说明这是如何工作的吗?我发现很难使用这些功能,因为我不理解这个组件.
所以我知道下面的代码是有效的,但由于我对编码很新,我不明白为什么以及如何工作.如果有人能向我解释,那将有助于我理解编程.
代码如下:
public static double minValue(double[] times){
double minValue = times[0];
for(int i = 1; i < times.length; i++){
if (times[i] < minValue){
minValue = times[i];
}
}
minValue = minValue / 60;
return minValue;
}
Run Code Online (Sandbox Code Playgroud)
这里我不明白的是:double minValue = times[0];为什么这个值0?它也适用于其他数字,低于数组的长度.使用空格或大于数组长度的数字会返回错误,但必须有代码专门使用的原因0.
然后这个:
for(int i = 1; i < times.length; i++){
if (times[i] < minValue) {
minValue = times[i];
}
}
Run Code Online (Sandbox Code Playgroud)
究竟如何确定最小值?
我看到这个代码,我看到了这个_标志(在main()里面)...
我从来没有看过这个标志在使用中,从来没有在任何地方阅读,
它用于什么?
我有这个程序真的很慢.分析揭示了XGetImage中的瓶颈(不是我不能在循环中调用XGetImage).在阅读推荐的解决方案时,请调用XShmGetImage,但文档真的很糟糕.
我正在寻找如何调用XShmCreateImage,XShmGetImage和XShmSetImage的简单示例代码.
深度分析肯定会使XGetImage成为根本瓶颈而不是XPutImage(是的,我知道调用XFlush使分析准确),因此我可能会得出结论,对内存分配器的隐式调用实际上是缓慢但没有XGetImage的变体这让我传递一个预分配的XImage,除了XShmGetImage.无论如何,Shm功能的使用可能会进一步提高.
在更新期间对站点进行维护时,哪些被认为是最佳实践?我这么问是因为我不太喜欢在谷歌中拥有超过 60k 索引页面的网站抛出 404 标头,实际上告诉谷歌这些网站已经消失。我宁愿告诉谷歌该网站已经消失了几个小时,所以谷歌机器人应该在几个小时后回来,现在什么也不做。
刚刚在 Google 网站管理员官方博客上找到了这篇博文:http://googlewebmastercentral.blogspot.com/2011/01/how-to-deal-with-planned-site-downtime.html,直接来自来源!
当我必须为linux下的新数据或可执行文件命名时,我总是感到困惑,使用下划线或短划线来分隔名称中的单词.
我的问题:是否有任何标准或指南可以知道何时使用这个或那个?
如何使用VB 6读取硬盘卷序列号但不使用任何ActiveX控件或第三方附加组件?
我写了一小段代码。我正在使用 void * 指针来为 C 的基本数据类型创建一个交换函数。我写了这个函数的两个版本,因为字符串与其他基本数据类型有点不同(但也许我错了)。这个函数的版本对于 int、float 和 char 它有效,但是当我尝试将一个用于字符串时,Valgrind 向我报告了多个错误。这是主要的:
int main(){
int dimA=0,dimB=0;
char *v,*g,*str1="heygv",*str2="bag";
v=malloc(strlen(str1)+1);
strcpy(v,str1);
g=malloc(strlen(str2)+1);
strcpy(g,str2);
dimA=strlen(v);
dimB=strlen(g);
printf("\nX : %s %p ,Y : %s %p \n",v,v,g,g);
swapStr(v,g,dimA+1,dimB+1);
printf("\nX : %s %p ,Y : %s %p \n",v,v,g,g);
free(v);
free(g);
return 191;
}
Run Code Online (Sandbox Code Playgroud)
这是交换函数,它可以正常工作并正确交换字符串的值:
void* swapStr(void* a,void* b,int dimA,int dimB){
void* temp=malloc(dimA);
void* ptr;
memcpy(temp,a,dimA);
if(dimB>dimA){
ptr=realloc(a,dimB);
if(ptr==NULL){
printf("\nError 1 realloc in swapStr\n");
return;
}else a=ptr;
}
memcpy(a,b,dimB);
if(dimA>dimB){
ptr=realloc(b,dimA);
if(ptr==NULL){
printf("\nError 2 realloc in swapStr\n"); …Run Code Online (Sandbox Code Playgroud) 我试图理解为什么这有时有效,有时无效 - open 和 read 系统调用。
//open files
fd1=open(argv[1],"O_RDONLY");
if(fd1==-1){
printf("open file number 1 failed\n");
exit(0);
}
fd2=open(argv[2],"O_RDONLY");
if(fd2==-1){
printf("open file number 2 failed\n");
exit(0);
}
//read first byte
r1=read(fd1, &byte1, 1);
if(r1==-1){
printf("read 1 1 error\n");
close(fd1);
exit(0);
}
r2=read(fd2, &byte2, 1);
if(r2==-1){
printf("read 2 error\n");
close(fd2);
close(fd1);
exit(0);
}
Run Code Online (Sandbox Code Playgroud)
输出:
~/Desktop/os$ ./comp/out 1.txt 2.txt
open file number 1 failed
~/Desktop/os$ ./comp/out 1.txt 2.txt
read 1 1 error
~/Desktop/os$ ./comp/out 1.txt 2.txt
open file number 1 failed
~/Desktop/os$ ./comp/out …Run Code Online (Sandbox Code Playgroud)