【问题标题】:ArrayIndexOutOfBoundsException converting C# to JavaArrayIndexOutOfBoundsException 将 C# 转换为 Java
【发布时间】:2015-03-23 06:10:35
【问题描述】:

我正在开发一个程序,该程序将遍历记录列表(ID 和门票)并将它们分别解析为两个列表。它还将交叉搜索列表以查看哪些 ID 具有基于名称的相应票证。这是早期版本的链接:here 现在,我一直在同事的一些 C# 代码的帮助下重写,但我在解析方法上遇到了问题。这是 C# 版本:

        public void parseLine(string _line)
    {
        if(string.IsNullOrWhiteSpace(_line)){ return;}

        code        = _line.Substring(0, 3);
        ticketID    = _line.Substring(3, 10);

        string tmp = _line.Substring(13).Trim();

        //get the first and last name
        string [] tmp1 = tmp.Split(",".ToCharArray());

        if(!(tmp1.Length > 1))
        {
            throw new Exception("unable to get the first and last name");
        }

        lastname        = tmp1[0];
        firstname       = tmp1[1].Trim();

    }

这是我的 Java 版本:

    public void parseLine(String line) throws Exception {
            // code will store Ticket code *Alpha ticketId will store 10
            // *digit code
    code = line.substring(0, 3);
    ticketId = line.substring(3, 10);
    
            // tmp will store everything afterthe first 13 characters of 
            // line and trim the name(s)
    String tmp = line.substring(13).trim();
    
            // tmp1 array
    String[] tmp1 = tmp.split(".*,.*");
    
    if (tmp1.length > 1) {
        
        throw new Exception("UNABLE TO GET NAME");
        
    }
    
    last = tmp1[0];
    first = tmp1[1].trim();
}
    

这是一个单独的类,它将为有票的人建模。我调用实际 parseLine 方法的主类(到目前为止)如下:

public class ParkingTickets {

public static void main(String[] args) throws 
        FileNotFoundException, Exception {

ArrayList<TicketPeople> tickets = new ArrayList<>();
HashMap<String, List<SbPeople>> people = new HashMap<>();
File srcFile = new File("source.txt");

Scanner myScanner = new Scanner(srcFile);
    
    while (myScanner.hasNextLine()) {
    String line = myScanner.nextLine();
    //System.out.println(line);
    if (line.matches("^\\p{Alpha}.*$")) {
    //System.out.printf("Ticket: %s%n", line);
        TicketPeople t = new TicketPeople();
        t.parseLine(line);
        tickets.add(t);
        }
        myScanner.close();
    }

}

}

编译器指向 parseLine 方法中的 if 语句,显然是主类中的 parseLine 方法,当我尝试单步执行该 sepcifiv 行时,我看到它开始解析源文件中的数据,但有些问题.从文档中,错误意味着:抛出以指示已使用非法索引访问了数组。索引为负数或大于或等于数组的大小。 我使用 ArrayList 作为票证列表,据我了解,它是一个动态列表,不需要设置特定的索引大小。我仍在学习,无法理解此异常。我将不胜感激。

【问题讨论】:

    标签: java c# arraylist


    【解决方案1】:

    您在 Java 中对 split() 的调用与 C# 中的 split() 不匹配。

    // String[] tmp1 = tmp.split(".*,.*");
    String[] tmp1 = tmp.split(","); // <-- ",".
    

    另外,您的检查逻辑似乎已被颠倒。但是,我会建议

    if (tmp1.length != 2) {
        throw new Exception("UNABLE TO GET NAME");
    }
    

    【讨论】:

    • 我试图记住为什么我在 Java 代码中更改了 split()。我想我正在尝试编写一个正则表达式来说明“,”分隔符之前或之后的任何内容。而且我对条件的逻辑肯定是关闭的,它应该说明 tmp1 是否小于或等于 1 ...正确?那么我的问题是,如果我使用 (tmp1.length != 2) 会更好更准确/更有效的条件吗?
    • @Luis 我想是的。你想要两个(而且只有两个元素)。
    【解决方案2】:

    1) 在 C# 中它是 Substring(int startIndex, int length)。在 java 字符串子字符串(int startindex,int endindex)中。

    2)java代码也改变了异常逻辑。在 C# 代码中存在 not if(!(tmp1.Length > 1)),而在 java 代码中,if (tmp1.length > 1)

    【讨论】:

    • #2 是避免使用! 的原因之一,因为有一种简单的方法可以否定条件。 C# 版本应该写成if (temp1.Length &lt;= 1),然后再犯这个错误就难多了。
    猜你喜欢
    • 1970-01-01
    • 2013-11-29
    • 2014-02-05
    • 2012-09-23
    • 2012-10-23
    • 2016-02-27
    • 2016-04-14
    • 2014-05-27
    • 2013-04-22
    相关资源
    最近更新 更多