根据Java平台标准版HotSpot虚拟机垃圾收集调整指南标题为"Generations"的部分
当年轻一代填满时,它会导致一个小集合,其中只收集年轻一代
和:
最终,终生代将填满并且必须被收集,从而产生一个主要的集合,其中收集整个堆.
因此,如果应用程序已完成分配阶段,并且在整个应用程序执行期间,年轻和终身的代数永远不会被填充(根本就是在任何阶段),那么gc甚至不会发生一次?
如果它会发生,原因是什么?因为这似乎与链接文档相反.
我有一个List的String(一个或多个),但我想它转换成一个Map<String, Boolean>从List<String>,使所有的boolean设置映射真实.我有以下代码.
import java.lang.*;
import java.util.*;
class Main {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("ab");
list.add("bc");
list.add("cd");
Map<String, Boolean> alphaToBoolMap = new HashMap<>();
for (String item: list) {
alphaToBoolMap.put(item, true);
}
//System.out.println(list); [ab, bc, cd]
//System.out.println(alphaToBoolMap); {ab=true, bc=true, cd=true}
}
}
Run Code Online (Sandbox Code Playgroud)
有没有办法减少使用流?
检查字符串是否仅包含空字符的正确方法是什么?
String s = "\u0000";
if(s.charAt(0) == 0) {
System.out.println("null characters only");
}
Run Code Online (Sandbox Code Playgroud)
或者
String s = "\0\0";
for(int i = 0; i < s.length(); i++) {
if(s.charAt(i) == 0)
continue;
else break;
}
Run Code Online (Sandbox Code Playgroud)
两者都有效。但是有没有更好,更简洁的方法来执行此检查。是否有实用程序可以检查 java 中的字符串是否仅包含空字符(\u0000OR \0)?
和之间有什么区别'\0'和'\u0000'?
我有一个csv文件,其中42,000行采用以下模式
03055,Milford,NH
03057,Mont Vernon,NH
03060,Nashua,NH
Run Code Online (Sandbox Code Playgroud)
我试图将数据存储在一个HashMap使用zipcode作为键,如
while ((line = stream_in.readLine())!=null) {
LocationBean temp_location_bean = new LocationBean();
String line_trimmed = line.trim();
String[] line_chunked = line_trimmed.split(",",4);
temp_location_bean.setZip_code(line_chunked[0]);
temp_location_bean.setCity(line_chunked[1]);
temp_location_bean.setState(line_chunked[2]);
this.locations_as_beans_list.put(zip_code, temp_location_bean);
}
Run Code Online (Sandbox Code Playgroud)
但是当我去查找时:
for(Map.Entry<String, LocationBean> location_object : this.locations_as_beans_list.entrySet())
{
LocationBean temp_location_bean = location_object.getValue();
if (params[0].matches(temp_location_bean.getZip_code())) {
master_location = temp_location_bean.getCity() + ","
+ temp_location_bean.getState()
+ ", (" + temp_location_bean.getZip_code() +")";
}
}
Run Code Online (Sandbox Code Playgroud)
它需要超过20秒....不应该表现相对较快?我怎样才能提高性能?
tl; dr 如何优化本例中的读取?
为什么此Java代码会引发StackOverflowError异常?
public class factorial2 {
public BigInteger fact( BigInteger n)
{
BigInteger one = new BigInteger("1");
if(n.equals("0"))
return one;
else
return n.multiply(fact(n.subtract(one)));
}
public static void main(String[] args) {
@SuppressWarnings("resource")
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
factorial2 f = new factorial2();
for(int i=0;i<n;i++)
{
BigInteger b = sc.nextBigInteger();
System.out.println(f.fact(b));
}
sc.close();
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试使用生成阶乘BigInteger。但是,为什么我的代码在输入时给出引用异常?
我可以static在不使用类名的情况下调用另一个类的方法(在同一个包中)吗?有类似的问题,但所有答案都使用类名.
我正在尝试使用来自".hgt"格式文件的二进制数据的应用程序.我在C++中找到了一个解决方案,但是我无法弄清楚如何将该解决方案转换为C.文件中的每个值都是带符号的short int类型,并且有1201x1201的值.
const int SIZE = 1201;
signed short int matrix[SIZE][SIZE] = {0};
int main(int argc, const char * argv[])
{
using namespace std;
ifstream file("N49E013.hgt", ios::in|ios::binary);
unsigned char buffer[2];
for (int i = 0; i < SIZE; ++i)
{
for (int j = 0; j < SIZE; ++j)
{
if(!file.read( reinterpret_cast<char*>(buffer), sizeof(buffer) ))
{
cout << "Error reading file!" << endl;
system("PAUSE");
return -1;
}
matrix[i][j] = (buffer[0] << 8) | buffer[1];
}
}
Run Code Online (Sandbox Code Playgroud) 我以下面显示的方式创建了一个数组; 代表3对坐标.我的问题是我似乎无法找到数组中特定坐标对的索引.
import numpy as np
R = np.random.uniform(size=(3,2))
R
Out[5]:
array([[ 0.57150157, 0.46611662],
[ 0.37897719, 0.77653461],
[ 0.73994281, 0.7816987 ]])
R.index([ 0.57150157, 0.46611662])
Run Code Online (Sandbox Code Playgroud)
返回以下内容:
AttributeError: 'numpy.ndarray' object has no attribute 'index'
Run Code Online (Sandbox Code Playgroud)
我试图这样做的原因是我可以在一个for循环中扩展一个带有坐标对的索引的列表.
例如
v = []
for A in R:
v.append(R.index(A))
Run Code Online (Sandbox Code Playgroud)
我只是不确定为什么索引功能不起作用,似乎无法找到解决方法.
我是编程的新手,请原谅我,如果这看起来像废话.
这已经困扰了我一段时间,我不确定是否有正确的答案:
拿这两个陈述
if(foo == bar)
do this;
else if(foo != bar)
do that;
Run Code Online (Sandbox Code Playgroud)
要么
if(foo != bar)
do that;
else if(foo == bar)
do this;
Run Code Online (Sandbox Code Playgroud)
其中一个是正确的(暂时忘记优化)我的头脑告诉我,答案取决于预期的结果,例如,如果这是在一个循环中运行,我预测foo将等于bar的次数比不是然后第一个是正确的.
这些符号之间是否存在任何效率问题?
我试图确定是否可以修改Java使用的拆箱方法.这样做的目的是能够做到:
class IInteger extends Integer {
@Override
public int unboxToPrimitive(){
return ++val;
}
}
IInteger i = 10;
System.out.print(i); // 11
Run Code Online (Sandbox Code Playgroud)
也就是说,重载方法并让它返回++ val而不是val.
java ×8
arrays ×2
hashmap ×2
java-8 ×2
android ×1
biginteger ×1
c ×1
c++ ×1
char ×1
class ×1
if-statement ×1
integer ×1
java-stream ×1
numpy ×1
operators ×1
overloading ×1
python ×1
recursion ×1
string ×1
wrapper ×1