类型不匹配: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
最满意答案
逐行浏览文件并使用堆栈。 一些事情:
<?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.
相关问答
更多-
MySql - WAMP - 巨大的表非常慢(2000万行)(MySql - WAMP - Huge Table is very slow (20 million rows))[2022-05-04]
一些答案: 2千万行是MySQL的能力范围之内。 我研究了一个表中有超过5亿行的数据库。 重构表格可能需要几个小时,但只要有索引帮助,普通查询就不会成为问题。 您的笔记本电脑非常过时,并且无法用作高级数据库服务器。 这将需要很长时间来做一个表重组。 内存不足和通常较慢的笔记本电脑磁盘可能会限制你。 您可能也在使用MySQL的默认设置,这些设置适用于非常旧的计算机。 我不建议为每一列使用TEXT数据类型。 对于大多数这些列,您没有理由需要TEXT 。 不要在每一列上创建索引,特别是如果您坚持使用TEXT数据类 ... -
巨大的XML文件到文本文件(Huge XML file to text files)[2022-10-25]
什么是pageContent ? 它似乎是一个String 。 一个简单的优化就是使用StringBuilder来代替; 它可以附加字符串,而无需像String s += do那样创建全新的字符串副本(如果您对长度有所了解,也可以使用初始保留容量来减少内存重新分配和副本)。 连接String是一个缓慢的操作,因为字符串在Java中是不可变的; 每次你调用a += b它必须分配一个新的字符串,复制a字符串,然后将b复制到它的末尾; 使每个级联O(n)wrt。 两个琴弦的总长度。 追加单个字符也一样。 另一方面 ... -
数据库模型 - 大量(超过100万行)文本文件[关闭](Data Base Model - Huge (over 1 million lines) text files [closed])[2022-11-30]
逐行浏览文件并使用堆栈。 一些事情: startTransaction(); $stack = array(); $fh = fopen("my-file", "r"); $i = 0; while (($buffer = fgets($handle, 4096)) !== false) { if (!isset($buf ... -
数字为100万的数组(Array with 1 million number)[2022-12-24]
在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 ...
-
具有多行和单行的文本文件中的行计数(line count with in the text files having multiple lines and single lines)[2022-02-26]
两种情况下的列都不同。 为了使它通用,我编写了一个打印行的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 ...