小编use*_*221的帖子

将文件作为命令行参数传递并读取其行

这是我在互联网上找到的用于读取文件行的​​代码,我也使用eclipse,并在其参数字段中将文件名称作为SanShin.txt传递.但它会打印:

Error: textfile.txt (The system cannot find the file specified)
Run Code Online (Sandbox Code Playgroud)

码:

public class Zip {
    public static void main(String[] args){
        try{
            // Open the file that is the first 
            // command line parameter
            FileInputStream fstream = new FileInputStream("textfile.txt");
            BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
            String strLine;
            //Read File Line By Line
            while ((strLine = br.readLine()) != null)   {
              // Print the content on the console
              System.out.println (strLine);
            }
            //Close the input stream
            in.close();
            }catch (Exception e){//Catch exception if any
              System.err.println("Error: …
Run Code Online (Sandbox Code Playgroud)

java command-line file

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

关于随机值

可能重复:
获得总和为M的N个随机数

嗨,我有一个问题:

如何得到所有值的总和的随机值1.喜欢{0.5,0.5}{0.25,0.25,0.5}更多.这些值的数量每次都不同,一次可以23上面的例子一样!谢谢.

java random algorithm

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

运行时间与O(nlogn)匹配吗?

我写了一个类(贪心策略),起初我使用的是具有O(nlogn)的排序方法

Collections.sort(array,new SortingObjectsWithProbabilityField());

然后我使用插入方法, binary search tree 其中 O(h)h here is the tree height.

对于不同的 n,运行时间将是:

n,running time
17,515428
33,783340
65,540572
129,1285080
257,2052216
513,4299709
Run Code Online (Sandbox Code Playgroud)

我认为这是不正确的,因为增加n,运行时间应该几乎增加.

此方法将占用运行时间:

Exponent = -1;



for(int n = 2;n<1000;n+=Math.pow(2,exponent){

     for (int j = 1; j <= 3; j++) {
                Random rand = new Random();
                for (int i = 0; i < n; i++) {
                    Element e = new Element(rand.nextInt(100) + 1, rand.nextInt(100) + 1, 0);
                    for (int k = 0; k < i; …
Run Code Online (Sandbox Code Playgroud)

java performance

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

从java中的arraylist获取对象的字段

嗨,我有一个数组列表,有七个类型为"点"的对象

我的"Points"类有2个字段(1)int x,(2)int y.

如何打印此列表System.out.println?谢谢

java arraylist

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

我可以将数组复制到哈希表吗?

我有一个数组,它的元素是 float 。如何将此数组复制到哈希表?

谢谢

java arrays collections hashtable

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

IndexOutOfBoundsException异常

嗨,此代码将返回indexoutofboundsException,我真的不知道为什么?我想删除那些与pointlist其中的对象相同的对象list.

    public void listOfExternalPoints(List<Point> list) {
    System.out.println(list.size());
    System.out.println(pointList.size());
    int n = pointList.size();
    for (int i = pointList.size() - 1; i >= 0; i--) {
        for (int j = 0; j < list.size(); j++) {
            if (pointList.get(i)==(list.get(j))) {
                pointList.remove(i);
                n--;
            }
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

println的输出也将是:

54
62
Run Code Online (Sandbox Code Playgroud)

也是例外:

Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: 60, Size: 60
    at java.util.ArrayList.RangeCheck(ArrayList.java:547)
    at java.util.ArrayList.get(ArrayList.java:322)
    at ConvexHull.BlindVersion.listOfExternalPoints(BlindVersion.java:83)
Run Code Online (Sandbox Code Playgroud)

谢谢.

java exception

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

具有双倍的积分值

嗨,我有一个具有面板的UI,您可以使用鼠标在其上放置一些点,它是它的代码:

    double x = evt.getX();
    double y = evt.getY() + 20;
    Graphics g = getGraphics();
    g.setColor(Color.black);
    g.fillOval((int) x, (int) y, 10, 10);
    Point point = new Point(x, y);
    pointList.add(point);
Run Code Online (Sandbox Code Playgroud)

当你看到我的"x"字段和"y"字段有双重类型但是当我打印这些点时,它们的值将是int,我如何保持它们的值并在我的双重类型函数中使用它们.当我打印它们时:

[120.0 ,134.0]
[345.0,785.0]
Run Code Online (Sandbox Code Playgroud)

java double

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

将树保存为预订

我创建了一个带{2,5,3,4,9,1,7,...,100}数字的二叉搜索树.

我怎么能把它保存为preorder?谢谢

编辑:考虑我有{ 3,7,1,2}binary search tree使用这些数字,我想保存这棵树preorder which is {3,1,2,7}

java binary-tree

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

双重链表

嗨,我想知道如何将我的对象从arrayList复制到双向链表?我的DNode构造函数也是:

    public DNode(Object element, DNode prev, DNode next) {
    this.element = element;
    this.next = next;
    this.prev = prev;
}
Run Code Online (Sandbox Code Playgroud)

即当我写这样的代码时,我的程序不起作用:

  DNode node = new DNode(pointList.get(0),null, null);

        for (int i = 1; i < pointList.size(); i++) {
        DNode dNode = new DNode(pointList.get(i), node, null);
        dList.addLast(dNode);
        }
Run Code Online (Sandbox Code Playgroud)

我也写了双链表,其中包含addAfter和addBefore方法以及更多内容.

java collections

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

在for循环中使用"i ++"

嗨,我有一个问题,我可以使用这样的代码:

        if (low != mid && mid != high) {
        for (int i = 0; i <= mid; i++) {
            boolean bool = Determinate.isPointLeftSide(a, auxiliaryListTwo.get(i), auxiliaryListTwo.get(i + 1));
            if (bool == false) {
                p = auxiliaryListTwo.get(i);

            } else {
                boolean bool1 = Determinate.isPointRightSide(a, auxiliaryListTwo.get(i + 1), auxiliaryListTwo.get(i));
                boolean bool2 = Determinate.isPointRightSide(a, auxiliaryListTwo.get(i + 1), b);
                if (bool1 == true && bool2 == true) {
                    p = auxiliaryList.get(i + 1);
                }
                else{
                    i++;
                }
            }

        }

    }
Run Code Online (Sandbox Code Playgroud)

我在else部分使用了"i ++",这是正确的吗?

java for-loop

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