Map.clear()然后将地图添加到List(Map.clear() then add map to List)
我尝试使用基于jdk1.6的 poi.jar来遍历一些excel2007数据 。但是当我发现一个奇怪的现象时,我遍历行(由HashMap ()存储),然后将行数据添加到java.util 。 ArrayList 。
并且启动下一个迭代器,我首先通过调用Map.clear()清除行数据,但是当再次调用ArrayList.add()方法时,此行数据将覆盖旧数据。
Map<String, String> cellForRow = = new HashMap<String, String>(); List<Map<String, String>> rowForSheet = new ArrayList<Map<String, String>>(); for (int j = 0; j < sheet.getPhysicalNumberOfRows(); j++) { row = sheet.getRow(j); if (j == 0) {// the first row is title titleRow = row; continue; } if (row != null && titleRow.getPhysicalNumberOfCells() > 0) { // cellForRow = new HashMap<String, String>(); cellForRow.clear(); for (int k = 0; k < titleRow.getPhysicalNumberOfCells(); k++) {// cell cellForRow.put(getCellValue(titleRow.getCell(k)), getCellValue(row.getCell(k))); } } rowForSheet.add(cellForRow); }
Next Snippets显示rowForSheet (List)的调试日志
[{ Up =Stream, Email=XXX,Down =Stream}, { Up =Stream, Email=XXX,Down =Stream}, { Up =Stream, Email=XXX,Down =Stream}, { Up =Stream, Email=XXX,Down =Stream}, { Up =Stream, Email=XXX,Down =Stream}]
后来的数据会覆盖旧数据
你是否?
I try to traverse some excel2007 data by using poi.jar based on jdk1.6.But when I seem to find a strange phenomenon that when I traverse the row(stored by HashMap()) and then add the row data to java.util.ArrayList .
And starting the next iterator,I first clear the row data by invoking Map.clear(), but when again invoking the ArrayList.add() method,this row data is overridden the older data.
Map<String, String> cellForRow = = new HashMap<String, String>(); List<Map<String, String>> rowForSheet = new ArrayList<Map<String, String>>(); for (int j = 0; j < sheet.getPhysicalNumberOfRows(); j++) { row = sheet.getRow(j); if (j == 0) {// the first row is title titleRow = row; continue; } if (row != null && titleRow.getPhysicalNumberOfCells() > 0) { // cellForRow = new HashMap<String, String>(); cellForRow.clear(); for (int k = 0; k < titleRow.getPhysicalNumberOfCells(); k++) {// cell cellForRow.put(getCellValue(titleRow.getCell(k)), getCellValue(row.getCell(k))); } } rowForSheet.add(cellForRow); }
Next Snippets show the debug log for rowForSheet(List)
[{ Up =Stream, Email=XXX,Down =Stream}, { Up =Stream, Email=XXX,Down =Stream}, { Up =Stream, Email=XXX,Down =Stream}, { Up =Stream, Email=XXX,Down =Stream}, { Up =Stream, Email=XXX,Down =Stream}]
the later data override the older data
Did you?
原文:https://stackoverflow.com/questions/40523285
最满意答案
正如我的评论中提到的,我强烈怀疑服务器关闭了通过putty建立的连接,因为您的程序存在问题。
但是这里有程序代码的几个问题。
最关键的问题是您是从两个线程同时访问两个全局变量。
需要保护并发访问。 为此,请声明两个互斥量,如下所示:
pthread_mutex_t mutex_read = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_t mutex_time = PTHREAD_MUTEX_INITIALIZER; void *readFromFile(void *myFile) { ... { int result = pthread_mutex_lock(mutex_read); if (0 != result) { perror("pthread_mutex_lock(mutex_read) failed"); } } read = 1; { int result = pthread_mutex_unlock(mutex_read); if (0 != result) { perror("pthread_mutex_unlock(mutex_read) failed"); } } ... } void displayTimeLeft(void *arg) { ... { int result = pthread_mutex_lock(mutex_time); if (0 != result) { perror("pthread_mutex_lock(mutex_time) failed"); } } timeLeft= 1; { int result = pthread_mutex_unlock(mutex_time); if (0 != result) { perror("pthread_mutex_unlock(mutex_time) failed"); } } ... } int main(void) { ... while(1) { int bread = 0; int btimeLeft = 0; { int result = pthread_mutex_lock(mutex_read); if (0 != result) { perror("pthread_mutex_lock() failed"); } } bread = (1 == read); { int result = pthread_mutex_unlock(mutex_read); if (0 != result) { perror("pthread_mutex_unlock() failed"); } } { int result = pthread_mutex_lock(mutex_time); if (0 != result) { perror("pthread_mutex_lock() failed"); } } btimeLeft = (1 == timeLEft); { int result = pthread_mutex_unlock(mutex_time); if (0 != result) { perror("pthread_mutex_unlock() failed"); } } if (bread == 1) { pthread_cancel(thread2); pthread_cancel(thread1); break; } else if (btimeLeft == 1) { pthread_cancel(thread1); pthread_cancel(thread2); break; } } ...
对于PThreads,thr threas函数需要声明为:
void * (*) (void *)
对于
displayTimeLeft
,情况并非如此
当在“字符串”中扫描时,需要传递表示字符串的字符数组的第一个元素的地址。 所以这
scanf("%s", &answer);
应该是这个
scanf("%s", &answer[0]);
或这个
scanf("%s", answer);
代码错过了
sleep()
的原型,所以添加#include <unistd.h>
完成此操作后,编译器会检测系统调用
read()
与代码声明的全局变量read
之间的名称冲突。 这不好。 将read
重命名为readit
。
最后并非最不重要的是,在他的回答中,嫌疑人有问题。 您滥用指向
int
的指针来存储一些时间值(int * time = 180
)。 不要那样做。要修复这个,请执行以下操作:
int main(void) { ... int time = 180; ... ret2 = pthread_create(&thread2, NULL, displayTimeLeft, &time);
并在
displayTimeLeft
执行:int time = *((int*) arg);
As mentioned in my comment I strongly doubt the server shuts down the connection established via putty because issues with your program.
However here are several issues with the program's code.
The ost critical issue is you are accessing the two global variable concurrently from two threads.
Concurrent access needs to be protected. To do so declare two mutexes like so:
pthread_mutex_t mutex_read = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_t mutex_time = PTHREAD_MUTEX_INITIALIZER; void *readFromFile(void *myFile) { ... { int result = pthread_mutex_lock(mutex_read); if (0 != result) { perror("pthread_mutex_lock(mutex_read) failed"); } } read = 1; { int result = pthread_mutex_unlock(mutex_read); if (0 != result) { perror("pthread_mutex_unlock(mutex_read) failed"); } } ... } void displayTimeLeft(void *arg) { ... { int result = pthread_mutex_lock(mutex_time); if (0 != result) { perror("pthread_mutex_lock(mutex_time) failed"); } } timeLeft= 1; { int result = pthread_mutex_unlock(mutex_time); if (0 != result) { perror("pthread_mutex_unlock(mutex_time) failed"); } } ... } int main(void) { ... while(1) { int bread = 0; int btimeLeft = 0; { int result = pthread_mutex_lock(mutex_read); if (0 != result) { perror("pthread_mutex_lock() failed"); } } bread = (1 == read); { int result = pthread_mutex_unlock(mutex_read); if (0 != result) { perror("pthread_mutex_unlock() failed"); } } { int result = pthread_mutex_lock(mutex_time); if (0 != result) { perror("pthread_mutex_lock() failed"); } } btimeLeft = (1 == timeLEft); { int result = pthread_mutex_unlock(mutex_time); if (0 != result) { perror("pthread_mutex_unlock() failed"); } } if (bread == 1) { pthread_cancel(thread2); pthread_cancel(thread1); break; } else if (btimeLeft == 1) { pthread_cancel(thread1); pthread_cancel(thread2); break; } } ...
For PThreads thr threas function needs to be declared as:
void * (*) (void *)
This isn't the case for
displayTimeLeft
When scanning in a "string" one need to pass the address of the first element of the character array representing the string. So this
scanf("%s", &answer);
should be this
scanf("%s", &answer[0]);
or this
scanf("%s", answer);
The code misses the protoype for
sleep()
, so add#include <unistd.h>
Having done so the compiler detects a name clash between the system call
read()
and the global variableread
declared by your code. This is not nice. Renameread
to something likereadit
.
Last not least there is the issue mentioend by suspectus in his answer. You are misusing a pointer to an
int
to store some time value (int * time = 180
). Do not do that.To fix this do like so:
int main(void) { ... int time = 180; ... ret2 = pthread_create(&thread2, NULL, displayTimeLeft, &time);
and in
displayTimeLeft
do:int time = *((int*) arg);
相关问答
更多-
底层连接已关闭:连接意外关闭(The underlying connection was closed: The connection was closed unexpectedly)[2022-12-16]
在升级POCO生成器类以使用EF 5.0之后,这开始工作正常并且完美 This started working fine and perfect after upgrading the POCO generator classes to use EF 5.0 -
为什么这个rsync连接在Windows上意外关闭?(Why is this rsync connection unexpectedly closed on Windows? [closed])[2022-05-25]
我的伎俩是我有ssh冲突。 我的Windows路径上安装了Git,其中包括ssh。 cwrsync也安装ssh。 诀窍是制作批处理文件来设置正确的路径: rsync.bat @echo off SETLOCAL SET CWRSYNCHOME=c:\commands\cwrsync SET HOME=c:\Users\Petah\ SET CWOLDPATH=%PATH% SET PATH=%CWRSYNCHOME%\bin;%PATH% %~dp0\cwrsync\bin\rsync.exe %* 在W ... -
去除: _request.Content.Headers.ContentEncoding.Add("gzip"); _request.Content.Headers.ContentEncoding.Add("deflate"); 因为你没有发送gzipped内容 另外,我建议你看一下使用WireShak实际发送的代码 - 因为我确信HttpRequestMessage已经附加了你需要的大部分头文件。 All I needed to do was to forbid automatical request ...
-
必须解除端口5432的阻塞才能使psql正常工作,但我无法做到这一点。 切换到其他网络可能会有所帮助。 Port 5432 must be unblocked for psql to work and I was unable to do that. Switching to a different network may help.
-
在调用服务器之前确保工作网络 if(isNetworkAvailable){ new AttemptLogin().execute(); } 当没有收到数据时,json解析器无法解析数据并抛出异常。 网络可用性的方法可以是 public boolean isNetworkAvailable(Context context) { // TODO Auto-generated method stub boolean haveConnectedWifi = fal ...
-
感谢Alex, 它在禁用ProxyCreationEnabled后工作。 public List
GetAllOrders() { List orders = null; using (NorthwindEntities context = new NorthwindEntities()) { context.Configuration.ProxyCreationEnabled = fals ... -
正如我的评论中提到的,我强烈怀疑服务器关闭了通过putty建立的连接,因为您的程序存在问题。 但是这里有程序代码的几个问题。 最关键的问题是您是从两个线程同时访问两个全局变量。 需要保护并发访问。 为此,请声明两个互斥量,如下所示: pthread_mutex_t mutex_read = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_t mutex_time = PTHREAD_MUTEX_INITIALIZER; void *readFromFile(void * ...
-
检测关闭的网络连接(Detect closed network connection)[2023-10-22]
当应用程序以正确的方式关闭套接字时,它会发送包含0个字节的消息。 在某些情况下,您可能会收到SocketException表明出现了问题。 在第三种情况下,远程方不再连接(例如通过拔出网络电缆)而没有双方之间的任何通信。 如果最后发生这种情况,您必须将数据写入套接字以检测您是否无法再访问远程方。 这就是发明保持活动机制的原因 - 它们经常检查它们是否仍能与另一方通信。 查看您现在发布的代码:使用NetworkStream时,对它的Read操作将返回值0(字节)以指示客户端已关闭连接。 文档提到两者 “如果没 ... -
使用最新的TortoiseSVN / svn.exe客户端。 现在它是TortoiseSVN 1.9 。 检查您的防病毒软件。 将TortoiseSVN / svn.exe添加到此防病毒软件的例外列表中并暂时将其禁用。 看看它是否有帮助。 Use up-to-date TortoiseSVN / svn.exe client. Right now it's TortoiseSVN 1.9. Check your antivirus. Add TortoiseSVN / svn.exe to exclusio ...
-
每次运行方法时,您都需要从池中获取新连接(并确保在完成后将其返回池中)。 看看with_connection 。 这样的事情应该有效。 class Deal < ActiveRecord::Base has_many :deal_prizes, dependent: :delete_all after_create :create_dealprizes # Constants TIME_SET = Time.zone.now def create_dealprizes se ...