小编Sop*_*ner的帖子

Java整数二维矩阵的最佳结构?

在Java中存储2D整数矩阵的最佳方法是什么?

这个矩阵将从一个数据文件填充,该数据文件可能有不同的尺寸,因此初始化int M [] [] = new int [n] [m]的某种尺寸不起作用,因为我们不知道矩阵的大小和我们将迭代文件的行并从每行中提取整数(由内部的空格分隔).所以我想使用ArrayList的ArrayList来动态添加整数作为对象,但我不太清楚如何做到这一点.

同样重要的是选择最佳结构来存储这种矩阵的性能,因为我将迭代这个矩阵并进行一些计算.

java integer arraylist

4
推荐指数
2
解决办法
2万
查看次数

形成和排序一组正整数的最快策略

在Java中,更快的是:创建,填充然后对下面的一组int进行排序

int[] a = int[1000];
for (int i = 0; i < a.length; i++) {
    // not sure about the syntax
    a[i] = Maths.rand(1, 500) // generate some random positive number less than 500
}
a.sort(); // (which algorithm is best?)
Run Code Online (Sandbox Code Playgroud)

或者即时插入排序

int[] a = int[1000];
for (int i = 0; i < a.length; i++) {
    // not sure about the syntax
    int v = Maths.rand(1, 500) // generate some random positive number less than 500
    int p = findPosition(a, …
Run Code Online (Sandbox Code Playgroud)

java arrays sorting algorithm insertion-sort

4
推荐指数
1
解决办法
2647
查看次数

Java中的同步hashmap只读访问

在 Java 中,有 3 个线程想要访问(只读)一个不可变的 hashmap 来做某事。SynchronizedMap类是不是为此目的最快的解决方案?如果没有,那么使用什么会更快?

import com.carrotsearch.hppc.IntObjectMap;
import com.carrotsearch.hppc.IntObjectOpenHashMap;

public class abc {
    public static void main(String[] args) {

        final IntObjectMap<int[]> map = new IntObjectOpenHashMap<int[]>();
        for (int i = 0; i < 4; i++) {
            map.put(i, new int[] {1, 2, 3, 4, 5});
        }

        Thread[] threads = new Thread[3];

        class SynchronizedMap {

            private final Object syncObject = new Object();

            public final int[] read(int i) {

                final int[] value;

                synchronized (syncObject) {

                    // code that reads-only immutable …
Run Code Online (Sandbox Code Playgroud)

java multithreading hashmap synchronized readonly

4
推荐指数
1
解决办法
1571
查看次数

还有什么比从端到端迭代ArrayList更快的东西?

使用此Java代码:

    // create the items list
    List<Item> items = new ArrayList<Item>();
    // ... (add some elements into the list so that it is not empty)

    // iterating ArrayList<Item> from right to left we find the position
    // based on the `if` condition satisfied for an item property
    int pos = 0;
    for (int j = items.size() - 1; j >= 0; j--) {
        Item item = items.get(j);
        if (item.property <= 7) {
            pos = j + 1; break;
        }
    } …
Run Code Online (Sandbox Code Playgroud)

java arraylist deque

3
推荐指数
1
解决办法
1439
查看次数

加速HashSet和HashMap性能

在Java中,我有:

Set<Integer> set = new HashSet<Integer>();
callVoidMethod(set);
...
public static void callVoidMethod(Set<Integer> set) {

    Set<Integer> superset = new HashSet<Integer>(set);
    ...
    // I just added this loop to show that I'm adding quite a lot
    // well, it depends on conditions, sometimes I add nothing,
    // but it is unpredictable and do not know if add something
    for (int i = 0; i < 1000; i++) {
         ...
         if (conditionSatisfied) superset.add(someValue);
         ...
    }

}
Run Code Online (Sandbox Code Playgroud)

上面的代码是简化的,想法是通过引用将集合传递给void方法并创建集合的完整副本,以便我们能够向副本添加一些新元素(这里是超集)并且不要触摸设置为我们在退出void方法时不需要它.

我的代码适用于大量数据处理,如果没有更快的方法来制作副本,那么我想优化HashSet自身,例如我不需要Integers作为键,但更好的原始int …

java integer hashmap hashset

3
推荐指数
3
解决办法
3458
查看次数

将ArrayList <Integer>转换为int []的最快方法

可能重复:
如何在Java中将List <Integer>转换为int []?

在Java中,低于最快的解决方案:

public convert(ArrayList<Integer> IntegerList) {

    int s = IntegerList.size();
    int[] intArray = new int[s];
    for (int i = 0; i < s; i++) {
        intArray[i] = IntegerList.get(i).intValue();
    }
}
Run Code Online (Sandbox Code Playgroud)

java arrays int arraylist

3
推荐指数
2
解决办法
2万
查看次数

Java线程安全的只写hashmap

在我的Java类中,我包含一个 Hashmap变量(类属性)并运行一些Threads,它们只HashMap使用以下内容put():每次写入时它都会存储一个唯一的键(由设计完成).

synchronized类方法上的关键字是否只适用于安全条件?我的HashMap很简单而不是ConcurrentHashMap

java hashmap thread-safety writeonly

3
推荐指数
1
解决办法
2587
查看次数

在for循环中调用.length()比在循环之前调用慢吗?

在Java代码中,我认为:

for (int i = 0; i < s.length(); i++) {
    // do a lot of something
}
Run Code Online (Sandbox Code Playgroud)

比这慢:

int length = s.length();
for (int i = 0; i < length; i++) {
    // do a lot of something
}
Run Code Online (Sandbox Code Playgroud)

请告诉我,我是否正确.

java optimization for-loop micro-optimization

2
推荐指数
1
解决办法
114
查看次数

Gnuplot,图线颜色

在gnuplot中,我绘制如下图:

gnuplot> set title "Performance analysis" font ", 16"
gnuplot> set xlabel "Array size" font ", 14"
gnuplot> set ylabel "Time, milliseconds" font ", 14"
gnuplot> set xrange [0:25]
gnuplot> set yrange [0:6300]
gnuplot> set xtics (5, 9, 11, 13, 15, 17, 19, 21, 23)
gnuplot> set ytics (88, 296, 433, 835, 1067, 1516, 2592, 3920, 6214)
gnuplot> set style line 1 linecolor rgb "blue"
gnuplot> plot "file.dat" using 1:2 title "Graph" with lines
Run Code Online (Sandbox Code Playgroud)

它很好,但我的图形的线条颜色仍然是红色(默认),请你帮我设置为蓝色.

gnuplot

2
推荐指数
1
解决办法
5588
查看次数

在Java中未经检查的强制转换警告

Eclipse说:Type safety: Unchecked cast from Object to ObjectArrayList<Car>当我这样做时:

final ObjectArrayList<Car> icars = (ObjectArrayList<Car>) cars[i];
Run Code Online (Sandbox Code Playgroud)

其中cars定义为:

final Object[] cars = new Object[1000];
for (int i = 0; i < 1000; i++) {
    cars[i] = new ObjectArrayList<Car>();
}
Run Code Online (Sandbox Code Playgroud)

Eclipse建议添加@SuppressWarnings("unchecked")icarsobject.但是我已经在某处读过注释在Java中被弃用了,所以我应该保留原样吗?

java warnings casting

2
推荐指数
1
解决办法
1024
查看次数