我需要创建一个Set
初始值.
Set<String> h = new HashSet<String>();
h.add("a");
h.add("b");
Run Code Online (Sandbox Code Playgroud)
有没有办法在一行代码中执行此操作?例如,它对最终的静态字段很有用.
Long.parseLong("string")
如果字符串无法解析为long,则抛出错误.有没有办法比使用更快地验证字符串try-catch
?谢谢
我需要经常使用matrix_vector_mult()
矩阵与向量相乘,下面是它的实现.
问题:是否有一种简单的方法可以使它显着,至少两倍,更快?
备注:1)矩阵的大小约为300x50.它在运行期间不会改变.2)它必须适用于Windows和Linux.
double vectors_dot_prod(const double *x, const double *y, int n)
{
double res = 0.0;
int i;
for (i = 0; i < n; i++)
{
res += x[i] * y[i];
}
return res;
}
void matrix_vector_mult(const double **mat, const double *vec, double *result, int rows, int cols)
{ // in matrix form: result = mat * vec;
int i;
for (i = 0; i < rows; i++)
{
result[i] = vectors_dot_prod(mat[i], vec, cols);
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个格式为yyyyMMdd的字符串对象 .是否有一种简单的方法来获取具有相同格式的前一个日期的字符串?谢谢
可能重复:
任何人都可以在printf中解释这些未定义的行为(i = i ++ + ++ i,i = i ++等...)
增量值
我有两个双阵列x
和y
和整数i
.我的问题是声明是否:
double res = x[i] * y[i++];
Run Code Online (Sandbox Code Playgroud)
总是等于声明:
double res = x[i] * y[i];
i++;
Run Code Online (Sandbox Code Playgroud)
是否有可能某些编译器会x[i] * y[i++]
变成y[i++] * x[i]
,这显然会产生不同的结果?
我需要实施
File[] files = getFiles( String folderName, String ptrn );
Run Code Online (Sandbox Code Playgroud)
其中ptrn是一个命令提示符样式模式,如"*2010*.txt"
我熟悉FilenameFilter类,但无法实现,
public boolean accept(File dir, String filename)
因为String.matches()不接受这样的模式.
谢谢!
我有下面的代码,它只是从文件夹中读取所有文件.此文件夹中有20,000个文件.该代码在本地文件夹(d:/files
)上运行良好,但//robot/files
在读取大约1,000 - 2,000个文件后在网络路径()上失败.
更新:文件夹是彼此的副本.
是什么导致这个问题以及如何解决它?
package cef_debug;
import java.io.*;
public class Main {
public static void main(String[] args) throws Throwable {
String folder = args[0];
File[] files = (new File(folder)).listFiles();
String line;
for (int i = 0; i < files.length; i++) {
BufferedReader br = new BufferedReader(new FileReader(files[i]));
while ((line = br.readLine()) != null) {
}
br.close();
}
}
}
Run Code Online (Sandbox Code Playgroud)
从网络路径(//robot/files
)读取时出现以下错误:
Exception in thread "main" java.io.IOException: Too many open files
at java.io.FileInputStream.open(Native Method) …
Run Code Online (Sandbox Code Playgroud) 我熟悉matlabpool
和parfor
使用,但我仍然需要加快计算速度.
我的1GB网络中有一台功能更强大的电脑.两台计算机都有R2010b,并且具有相同的代码和路径.
使用两台计算机进行并行计算的最简单方法是什么?
我今天使用的代码示例:
--- main.m ---
matlabpool('open', 3);
% ...
x = randn(1e5,1);
y = nan(size(x));
parfor k = 1 : length(x)
y(k) = myfunc(x(k));
end
Run Code Online (Sandbox Code Playgroud)
--- myfunc.m ---
function y = myfunc(x)
y = x; % some computation
return
Run Code Online (Sandbox Code Playgroud) 我需要创作一部电影.假设,我创建了一个轴并在其上绘制了一些非常自定义的东西:
figure;
ax = plot(x, y, 'linewidth', 3, 'prop1', value1, 'prop2', value2, ...);
grid minor;
axis(ax, [xmin xmax ymin ymax]);
legend(ax, ...);
xlabel(ax, ...);
ylabel(ax, ...);
title(ax, ...);
Run Code Online (Sandbox Code Playgroud)
现在我运行一个循环,其中只y
更新值.
for k = 1 : N
% y changes, update the axis
end
Run Code Online (Sandbox Code Playgroud)
使用new y
(或x
和y
)更新轴的最快和最简单的方法是什么,保留所有轴属性?
我从来没有在java中编写GUI.这次我也可以跳过它并args
用作UI(用户界面).但我想知道是否有一种简单的方法可以创建一个小的GUI来让用户选择其中一个选项.换句话说,要实现askUser()
用户可以从下拉菜单中选择并按"确定"的功能.我花了一些时间学习这个主题,但甚至不确定我知道这个任务需要哪种类型的GUI.JFrame的?JPanel的?JMenu的?谢谢.
这是所需功能的一个例子.
package trygui;
public class Main {
public static void main(String[] args) {
String[] choices = new String[]{"cats", "dogs"};
int choice = askUser(choices);
System.out.println("selected: " + choices[choice]);
}
static int askUser(String[] choices) {
// create pop-up dialog
return 0;
}
}
Run Code Online (Sandbox Code Playgroud)
更新:我使用Netbeans,如果这可以有所作为.
java ×6
c++ ×2
matlab ×2
axis-labels ×1
c ×1
collections ×1
constructor ×1
date ×1
filenames ×1
filter ×1
hashset ×1
io ×1
jcombobox ×1
joptionpane ×1
matrix ×1
networking ×1
parsing ×1
performance ×1
popup ×1
regex ×1
string ×1
swing ×1
try-catch ×1