首页 \ 问答 \ 类型不匹配:boolean [],Boolean [],ArrayList (Type mismatch: boolean[], Boolean[], ArrayList)

类型不匹配:boolean [],Boolean [],ArrayList (Type mismatch: boolean[], Boolean[], ArrayList)

我试图从文本文件构造一个boolean [] []。 我正在读取每个字符,存储在ArrayList中(1s为true,0为false)。 我试过ArrayList,但得到了编译错误,意外的元素。 所以,我构建一个ArrayList,并尝试将其存储在ArrayList>中。 我的最后一个问题是在以下方法中布尔值和布尔值之间的类型差异。 seedInProgress是由finishLine()构造的ArrayList>。 我试图将它全部复制到for循环中的boolean [] []。

public void finishSeed(ArrayList<Boolean> lastLine) {
    finishLine(lastLine);
    seed = new boolean[seedInProgress.size()][seedInProgress.get(0).size()];
    for (int i = 0; i < seedInProgress.size(); i++ ) {
        seed[i] = seedInProgress.get(i).toArray()
    }
}

错误是:

SeedFactory.java:75: error: incompatible types
            seed[i] = seedInProgress.get(i).toArray();
                                                   ^
  required: boolean[]
  found:    Object[]
1 error

但我无法将其强制转换为boolean [],而Boolean []仍然是Object []。

如何从ArrayList获取boolean [],或者如何创建ArrayList?


I am trying to construct a boolean[][] from a text file. I am reading in each character, storing in an ArrayList (1s as true and 0s as false). I tried ArrayList, but got compile errors, unexpected element. So, I build an ArrayList, and try to store it in an ArrayList>. My last problem is with the type difference between boolean and Boolean in the following method. seedInProgress is the ArrayList> constructed by finishLine(). I am trying to copy it all out to a boolean[][] inside the for loop.

public void finishSeed(ArrayList<Boolean> lastLine) {
    finishLine(lastLine);
    seed = new boolean[seedInProgress.size()][seedInProgress.get(0).size()];
    for (int i = 0; i < seedInProgress.size(); i++ ) {
        seed[i] = seedInProgress.get(i).toArray()
    }
}

The error is:

SeedFactory.java:75: error: incompatible types
            seed[i] = seedInProgress.get(i).toArray();
                                                   ^
  required: boolean[]
  found:    Object[]
1 error

but I can't cast it to boolean[], and Boolean[] is still an Object[].

How can I get a boolean[] from an ArrayList, or alternatively, how can I make an ArrayList?


原文:https://stackoverflow.com/questions/18794254
更新时间:2023-12-05 22:12

最满意答案

逐行浏览文件并使用堆栈。 一些事情:

<?php

// You'd have to implement the database yourself!
$db = new Database();
$db->startTransaction();

$stack = array();
$fh    = fopen("my-file", "r");
$i     = 0;
while (($buffer = fgets($handle, 4096)) !== false) {
  if (!isset($buffer[0])) {
    continue;
  }

  switch ($buffer[0]) {
    case "A":
      // Do something ...
      break;

    case "C":
      // Do something ...
      break;

    case "C":
      if ($stack[$i] != "C") {
        trigger_error("Line D without preceding line C");
      }
      // Do something ...
      break;

     // More stuff ...
  }

  $stack[$i++] = $buffer;
  $db->insert("INSERT INTO table (line) VALUES ($buffer)");
}

$db->commitTransaction();

?>

当然,有比丑陋的switch更好的解决方案,但它很快就会出现问题。 您的数据库设计答案无法回答,因为我们对这些要求一无所知。 总而言之,考虑发布你的工作并询问有关一小部分大问题的具体问题,而不是要求解决大问题。


Go through the file line by line and use a stack. Something along the lines:

<?php

// You'd have to implement the database yourself!
$db = new Database();
$db->startTransaction();

$stack = array();
$fh    = fopen("my-file", "r");
$i     = 0;
while (($buffer = fgets($handle, 4096)) !== false) {
  if (!isset($buffer[0])) {
    continue;
  }

  switch ($buffer[0]) {
    case "A":
      // Do something ...
      break;

    case "C":
      // Do something ...
      break;

    case "C":
      if ($stack[$i] != "C") {
        trigger_error("Line D without preceding line C");
      }
      // Do something ...
      break;

     // More stuff ...
  }

  $stack[$i++] = $buffer;
  $db->insert("INSERT INTO table (line) VALUES ($buffer)");
}

$db->commitTransaction();

?>

Of course there are better solutions than the ugly switch, but it's quick'n'dirty. Your database design answer is impossible to answer because we have no clue about the requirements. All in all consider posting your work and ask specific questions regarding a small piece of a big problem and not asking to solve big problems.

相关问答

更多
  • 一些答案: 2千万行是MySQL的能力范围之内。 我研究了一个表中有超过5亿行的数据库。 重构表格可能需要几个小时,但只要有索引帮助,普通查询就不会成为问题。 您的笔记本电脑非常过时,并且无法用作高级数据库服务器。 这将需要很长时间来做一个表重组。 内存不足和通常较慢的笔记本电脑磁盘可能会限制你。 您可能也在使用MySQL的默认设置,这些设置适用于非常旧的计算机。 我不建议为每一列使用TEXT数据类型。 对于大多数这些列,您没有理由需要TEXT 。 不要在每一列上创建索引,特别是如果您坚持使用TEXT数据类 ...
  • 什么是pageContent ? 它似乎是一个String 。 一个简单的优化就是使用StringBuilder来代替; 它可以附加字符串,而无需像String s += do那样创建全新的字符串副本(如果您对长度有所了解,也可以使用初始保留容量来减少内存重新分配和副本)。 连接String是一个缓慢的操作,因为字符串在Java中是不可变的; 每次你调用a += b它必须分配一个新的字符串,复制a字符串,然后将b复制到它的末尾; 使每个级联O(n)wrt。 两个琴弦的总长度。 追加单个字符也一样。 另一方面 ...
  • 逐行浏览文件并使用堆栈。 一些事情: startTransaction(); $stack = array(); $fh = fopen("my-file", "r"); $i = 0; while (($buffer = fgets($handle, 4096)) !== false) { if (!isset($buf ...
  • 在SOAP请求中不可能使用指向您文件的链接。 SOAP请求必须包含要检索的所有标识符。 此外,对于该Web服务而言,一百万行可能太多,您需要将它们拆分为多个块。 要创建包含所有数字的SOAP请求,可以使用命令生成所有int元素。 然后,您可以使用任何文本编辑器将其包装到SOAP信封中。 这将为您提供可以发送到Web服务的请求。 在Linux环境中,生成行的命令将如下所示( identifiers.txt是包含所有标识符的文件的名称, a是http://schemas.microsoft.com/Arrays ...
  • 我建议每次使用蒸汽读取器并读取一行,然后处理它,然后在“扔掉”旧行的同时读取下一行。 这样您就不需要将每一行读入内存而只需读取工作行。 I'd suggest using a steam reader and read a single line each time and then handle that and then read the next line while 'throwing away' the old line. That way you do not need to read ever ...
  • 以下是对可以解决您的问题的架构的非常粗略的描述,假设当您有足够的实例时,文件描述符的最大数量是无关紧要的。 首先,看看这个: https://aws.amazon.com/blogs/aws/amazon-elastic-file-system-shared-file-storage-for-amazon-ec2/ https://aws.amazon.com/efs/ EFS提供了一个可以作为文件系统挂载的共享存储。 您可以将所有文件存储在EFS的单个存储单元中。 然后,您将需要一组N个工作机器,它们以文 ...
  • 恐怕这是不可能的,从gzip男人引用: 如果您希望创建一个具有多个成员的归档文件,以便以后可以独立提取成员,请使用tar或zip等归档文件。 更新:在编辑之后,如果gz只包含一个文件,像awk这样的一步工具就可以了: gzip -cd path/to/test/file.gz | awk 'BEGIN{global=1}/my regex/{count+=1;print $0 >"part"global".txt";if (count==1000000){count=0;global+=1}}' spli ...
  • 这里有一种懒惰的阅读Python中的大文件的方法。 您可以使用该方法并一次处理四行。 请注意,不需要执行四次读取操作,然后重复执行处理,然后重复四次读取操作。 您可以从文件中读取几百行或几千行文件,然后一次处理四行文本。 当你完成这些行时,你可以继续阅读文件的内容。 There is a method for lazily reading large files in Python here. You can use that approach and process four lines at a tim ...
  • SqlBulkCopy的。 阅读它。 在文档中。 更快 - 因为它不是真正的智能编写 - 是将它变成临时表,然后在插入到最终表的最后。 SqlBulkCopy锁定整个表,绕过它并允许在上载过程中使用该表。 然后使用多个线程插入每行超过10000行的块。 我在低端数据库服务器上管理超过100,000行 - 每秒 - (即48GB内存,大约十几个SAS光盘 - 是的,这是低端)。 SqlBulkCopy. Read about it. IN the documentation. FASTER - because ...
  • 两种情况下的列都不同。 为了使它通用,我编写了一个打印行的perl脚本。 它从头文件生成正则表达式并用它来计算行。 我假设第一行总是代表列数。 #!/usr/bin/perl -w open(FH, $ARGV[0]) or die "Failed to open file"; # Get coloms from HEADER and use it to contruct regex my $head = ; my @col = split(",", $head); # Colums arra ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)