小编Don*_*oby的帖子

这两种方法有什么区别?JAVA

我的教授为我提供了一系列方法来填写罗马数字程序(加法格式,所以4 = IIII,9 = VIIII等)

我无法理解这两种方法之间的区别:

   **
   * This method prints, to the standard output and in purely additive
   * notation, the Roman numeral corresponding to a natural number.
   * If the input value is 0, nothing is printed. This
   * method must call the method romanDigitChar(). 
   * @param val   ?
   */

    public void printRomanNumeral(int val)
    {

    }

   **
   * This method returns the Roman numeral digit corresponding
   * to an integer value. You must use a nested-if statement.
   * …
Run Code Online (Sandbox Code Playgroud)

java methods roman-numerals

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

使用 JAVA 从目录中检索所有 XML 文件名

我有一个包含多个文件的目录。我只需要使用 Java 检索列表中的 XML 文件名。我怎样才能做到这一点?

java directory java-io

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

在Scala中迭代涉及Java泛型<?>的java List

我有一个API(来自第三方java库),看起来像:

public List<?> getByXPath(String xpathExpr)

defined on a class called DomNode
Run Code Online (Sandbox Code Playgroud)

我在scala中尝试这个:

node.getByXPath(xpath).toList.foreach {node: DomElement => 

   node.insertBefore(otherNode)   

}
Run Code Online (Sandbox Code Playgroud)

但是我在node.getByXPath上遇到编译错误.错误:"类型不匹配;找到:(com.html.DomElement)=>所需单位:(?0)=>?其中type?0"

如果我将其更改为:

node.getByXPath(xpath).toList.foreach {node => 

   node.insertBefore(otherNode)   

}
Run Code Online (Sandbox Code Playgroud)

然后错误消失但是我在node.insertBefore(otherNode)错误上得到错误:"value insertBefore不是?0的成员"

这个问题的答案是什么?

scala

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

数组荒诞挑战

所以,我一直在研究以下codeeval挑战问题.

数组荒谬

描述:

想象一下,我们有一个大小为N的不可变数组,我们知道这些数组用0到N-2(包括0和N-2)的整数填充.假设我们知道该数组只包含一个重复的条目,并且该副本恰好出现两次.找到重复的条目.(对于奖励积分,确保您的解决方案具有与N成比例的恒定空间和时间)

输入样本:

您的程序应该接受文件名路径作为其第一个参数.此文件中的每一行都是一个测试用例.忽略所有空行.每行以正整数(N)开始,即数组的大小,然后是分号,后跟逗号分隔的正数列表,范围从0到N-2,包括0和N-2.即例如.

5;0,1,2,3,0
20;0,1,10,3,2,4,5,7,6,8,11,9,15,12,13,4,16,18,17,14
Run Code Online (Sandbox Code Playgroud)

输出样本:

打印出重复的条目,每个条目都在一个新行上,例如

0
4
Run Code Online (Sandbox Code Playgroud)

在文件中提交您的解决方案(某个文件名).(py | c | cpp | rb | pl | php | tcl | clj | js)| array_absurdity.java或使用在线编辑器.

我觉得这很容易.我对它进行了编码,在我的计算机上使用各种测试用例对其进行了测试,似乎工作正常.但是,当我在codeveal上提交问题时,我一直得到一个0.我已经想到了所有可能的测试用例但是为什么它一直失败.如果你们能提出一些想法,我将不胜感激.我编写的以下解决方案是在java中.

import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Scanner;
public class array_absurdity {

    public static int findDuplicate(int [] arr){
        int sumAll = 0;
        int sumEle = sumElements(arr.length-2);

        for(int i = 0; i < arr.length; i++){
            sumAll += arr[i];           
        }
        if(sumAll < sumEle)
            return 0;
        else            

        return (sumAll - …
Run Code Online (Sandbox Code Playgroud)

java

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

XML中的HTML标记

我想在XML文件中使用html标签,所以在我的xsd文件中,我允许html格式化:

        <xs:element name="description">
            <xs:complexType mixed="true">
                <xs:sequence>
                    <xs:any namespace="http://www.w3.org/1999/xhtml"
                            processContents="lax"
                            minOccurs="0"
                            maxOccurs="unbounded" />
                </xs:sequence>          
            </xs:complexType>
        </xs:element>
Run Code Online (Sandbox Code Playgroud)

在我的XML文件中

<description>
    <p xmlns="http://www.w3.org/1999/xhtml"> This course is an introduction to the information technologies required for secure, practical information systems for electronic commerce.
Topics will be chosen from areas such as document representation (XML, DTDs, XML Schema, XSLT, CSS), 
</p> security (encryption, public key, symmetric key, 
PKI, authentication); kinds of attack and vulnerabilities, electronic trading (spontaneous, deliberative, auctions), electronic document management 
(metadata, search, digital libraries, management …
Run Code Online (Sandbox Code Playgroud)

html xml xslt

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

Guava Iterables.filter(),你如何测试空回报?

我喜欢使用Guava的Iterables过滤器.但是一个常见的代码片段如下所示:

  List foo = Lists.newArrayList(Iterables.filter(someCollection, somePredicate));  
  if (foo.isEmpty) {  
    // do empty  
  } else {  
    int count = foo.size();  // do non-empty  
  }  
Run Code Online (Sandbox Code Playgroud)

这是浪费,因为我真的不需要构建"foo"列表,我只需要知道它是否为空,并获得元素数量的计数.

我想知道以下的最佳实践:
1)如何在isEmpty()不浪费时间的情况下将列表构建到内存中进行测试
2)有没有办法在不迭代所有条目的情况下获取大小?
3)如果#2没有非迭代解决方案,那么迭代并计算++会更好吗?

java guava

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

如何获取目录中创建的某些模式的最新文件

我可以通过以下方式获得最新创建的内容; 但我希望用最新创建的最新版本创建最新版本

 var directory = new DirectoryInfo("D:\\my_dir\\dir_1\\dir_2\\");
        var smf_log_name = directory.GetFiles().OrderByDescending(f => f.LastWriteTime).First();
Run Code Online (Sandbox Code Playgroud)

c# file

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

将1个字符串元素转换为整数

编写一个程序,检查String数组是否包含有效数字.如果字符串包含".",则将其转换为Double,否则将其转换为Integer.Input应该是字符串数组{"10.20","123456","12 .无效"}.

我的问题是123456变为double.我必须将其更改为int.Please help :(

public class Ch7LU3Ex1
{
    public static void main(String[] args)
    {
        String[] str = new String []{"10.20","123456","12.invalid"};
        int i,count=0;
        try
        {
            for(i=0;i<3;i++)
            {
                int l = str[i].length();
                for(int j=0;j<l;j++)
                {
                    if(str[i].charAt(j)=='.')
                    {
                        count++;
                    }
                    else
                    {
                        continue;
                    }
                }
                if(count!=0)
                {
                    double d = Double.parseDouble(str[i]);
                    System.out.println(d);
                }
                else
                {
                    int e = Integer.parseInt(str[i]);
                    System.out.println(e);
                }
            }
        }
        catch(NumberFormatException e)
        {
            System.out.println("Invalid number");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

java

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

无法比较两个阵列

我正在玩一些代码,我无法弄清楚为什么我的equals方法在两个数组相等时返回false.

public class Equal {

    static int[] a;

    public static boolean equals(int[] b){
        for(int i=0;i<b.length;i++){
            if(a[i] != b[i]) return false;
        }
        return true;
    }

    public static void main(String[] args){
        int[] a = new int[3];
        a[0]=1; a[1]=2; a[2]=3;

        int[] b = new int[3];
        b[0]=1; b[1]=2; b[2]=3;

        System.out.println(a.equals(b)); //returns false (but why??)
        System.out.println(Arrays.equals(a,b)); //returns true as expected
    }
}
Run Code Online (Sandbox Code Playgroud)

java

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

运行列表时出现堆栈溢出错误

我正在研究数据结构,我必须自己做一个双链表,创建Node和List对象而不是使用Java实现LikedList,每次我尝试添加2个元素时,我都会遇到Stack Overflow错误列表,我不知道为什么.请你帮助我好吗?

public class DoubleLinked {
    Node head;
    Node tail;
    int size;

    public DoubleLinked() {
        head = tail = null;
        size = 0;
    }

    public void insertOnHead(int x) {
        Node newNode = new Node();
        newNode.value = x;
        if(head == null) {
            head = newNode;
            size++;
        } else {
            newNode.next = head;
            head.previous = newNode;
            head = newNode;
            size++;
        }
    }

    @Override
    public String toString() {
        return "[Head: " + head + ", Tail: " + tail + ", Size: " …
Run Code Online (Sandbox Code Playgroud)

java data-structures

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

标签 统计

java ×7

c# ×1

data-structures ×1

directory ×1

file ×1

guava ×1

html ×1

java-io ×1

methods ×1

roman-numerals ×1

scala ×1

xml ×1

xslt ×1