本文介绍了添加一个字符串'临时'价值ArrayList的每一个新的价值,而不会覆盖previous'临时'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我输入从插座使用 DataInputSteam 来了,因为我可以有几种不同的字符串值都被分配以同样的 clientDayOfWeek 字符串,我无法弄清楚如何将所有进来到同一个ArrayList中的字符串值保存,无需更换的最后一个值。我也想没有重复,如果可能的。

  Socket套接字= NULL;
       DataInputStream以DataInputStream以= NULL;
       DataInputStream以=新DataInputStream所(
                              socket.getInputStream());       串clientDayOfWeek = dataInputStream.readUTF();
       ArrayList的<串GT; AR =新的ArrayList<串GT;();
       字符串TEMP = clientDayOfWeek;
       ar.add(临时);
       System.out.print(列表中的项目:+ AR);


解决方案

感谢Prasaanth,这就是我在做什么错。

我需要我的的ArrayList<串GT; AR =新的ArrayList<串GT;(); 是全球性的,简化了剩下的我的方法里面如下:

  DataInputStream以=新DataInputStream所(
                        socket.getInputStream());
   ar.add(dataInputStream.readUTF());
   System.out.print(芳:+ AR);

My input is coming from a socket using DataInputSteam and because I can have several different String values all being assigned to same clientDayOfWeek string, I cannot figure out how to save all the string values coming in into the same ArrayList without replacing the last value. I'd also like no duplicates if possible.

       Socket socket = null;
       DataInputStream dataInputStream = null;
       dataInputStream = new DataInputStream(
                              socket.getInputStream());

       String clientDayOfWeek = dataInputStream.readUTF();
       ArrayList<String> ar = new ArrayList<String>();
       String temp = clientDayOfWeek;
       ar.add(temp);
       System.out.print("Items in list: "+ ar);
解决方案

Thanks Prasaanth, that's what I was doing wrong.

I needed my ArrayList<String> ar = new ArrayList<String>(); to be global and simplified the rest as follows inside my method.

dataInputStream = new DataInputStream(
                        socket.getInputStream());
   ar.add(dataInputStream.readUTF());
   System.out.print("ar: "+ar);

这篇关于添加一个字符串'临时'价值ArrayList的每一个新的价值,而不会覆盖previous'临时'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 09:23