[英]ArrayList combination to one String
Updated: 更新:
package packet;
import java.util.ArrayList; //java library implementation of List interface
import java.util.Arrays;
import java.util.List; //java library the List interface
import java.util.Random; //for random number generator
import java.io.FileNotFoundException; //for opening a file
import java.io.PrintWriter; //for writing to a file
public class Message
{
private String message;
private int packetLength;
private List<Packet> dividedMessage; //use java library java.util.List
private boolean messageSent;
private Random generator; //use java library java.util.Random
public Message(String theMessage, int thePacketLength)
{
message = theMessage;
packetLength = thePacketLength;
receiveInFile(message);
}
public void send()
{ // divide the message into Packet and store the packets in the list
// dividedMessage
dividedMessage = new ArrayList<Packet>();
List<String> items = Arrays.asList(message.split("\\s*,\\s*"));
int sequenceNumber = packetLength;
String nextPiece = message;
//This is the line of code to create a Packet object using a sequence
//number and a string (represent part of the message)
Packet nextPacket = new Packet(sequenceNumber, nextPiece);
dividedMessage.add(nextPacket);
}
/** Simulates the receipt of the packets of a message. The packets
are not in any particular order and are written to a text file.
@param fileName a string that names the file to be created and the
packets are written to the file.
@return true if the packets are received, or
false if the message was never sent or the file
cannot be created */
public boolean receiveInFile(String fileName)
{
String[] items = message.split(" ");
int numberOfPackets = items.length;
dividedMessage = Arrays.asList(message.split(","));
shuffle(dividedMessage, numberOfPackets);
// container.send();
return messageSent;
}
public String toString()
{
return message;
}
private void shuffle (List<Packet> packetList, int numberOfPackets)
{
if (numberOfPackets > 1)
{
// swap packet at index numberOfPackets - 1 and a random index
Packet temp = packetList.get(numberOfPackets - 1);
int randomIndex = Math.abs(generator.nextInt())
%numberOfPackets;
packetList.set(numberOfPackets - 1, packetList.get(randomIndex));
packetList.set(randomIndex, temp);
shuffle(packetList, numberOfPackets - 1);
} // end if
} // end Shuffle
}// end class Message
Original: 原版的:
I am making a sample program with the intent of sending packets out of order and reorders then outputs the packets in a full string. 我正在制作一个示例程序,目的是不按顺序发送数据包,然后重新排序,然后以完整的字符串输出数据包。 i have the ordering part working fine so i did not include them, i do not seem to know where to go next from here, these classes should be taking the reordered packets and make them into one string. 我的排序部分工作正常,所以我没有包括它们,我似乎也不知道从这里下一步去哪里,这些类应该将重新排序的数据包放入一个字符串中。 any help is awesome! 任何帮助都很棒!
public class Packet implements Comparable<Packet>
{
private int sequence; //sequence number of the packet
private String message; //a part of the message represented by the packet
public Packet(int sequenceNumber, String nextPiece) {
setSequence(sequenceNumber);
setMessage(nextPiece);
Message test = new Message(message, sequence);
}
@Override
public int compareTo(Packet o) {
int comparedSize = o.sequence;
if (this.sequence > comparedSize) {
return 1;
} else if (this.sequence == comparedSize) {
return 0;
} else {
return -1;
}
}
public String toString() {
return message;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public int getSequence() {
return sequence;
}
public void setSequence(int sequence) {
this.sequence = sequence;
}
//Implement constructor and other methods as necessary
}
import java.util.ArrayList; //java library implementation of List interface
import java.util.List; //java library the List interface
import java.util.Random; //for random number generator
import java.io.FileNotFoundException; //for opening a file
import java.io.PrintWriter; //for writing to a file
public class Message
{
private String message;
private int packetLength;
private List<Packet> dividedMessage; //use java library java.util.List
private boolean messageSent;
private Random generator; //use java library java.util.Random
/**implement constructor */
public Message(String theMessage, int thePacketLength)
{
message = theMessage;
packetLength = thePacketLength;
}
/** Sends this message as a sequence of packets. */
public void send()
{ // divide the message into Packet and store the packets in the list
// dividedMessage
dividedMessage = new ArrayList<Packet>();
int sequenceNumber = 0;
String nextPiece = null;
//This is the line of code to create a Packet object using a sequence
//number and a string (represent part of the message)
Packet nextPacket = new Packet(sequenceNumber, nextPiece);
}
/** Simulates the receipt of the packets of a message. The packets
are not in any particular order and are written to a text file.
@param fileName a string that names the file to be created and the
packets are written to the file.
@return true if the packets are received, or
false if the message was never sent or the file
cannot be created
*/
public boolean receiveInFile(String fileName)
{
// shuffle the packets to simulate their arrival order
int numberOfPackets = 0;
shuffle(dividedMessage, numberOfPackets);
return messageSent;
}
public String toString()
{
return message;
}
private void shuffle (List<Packet> packetList, int numberOfPackets)
{
if (numberOfPackets > 1)
{
// swap packet at index numberOfPackets - 1 and a random index
Packet temp = packetList.get(numberOfPackets - 1);
int randomIndex = Math.abs(generator.nextInt())
%numberOfPackets;
packetList.set(numberOfPackets - 1, packetList.get(randomIndex));
packetList.set(randomIndex, temp);
shuffle(packetList, numberOfPackets - 1);
} // end if
} // end Shuffle
}// end class Message
The original message is "Meet me at 6 o'clock in the union." 原始消息是“在工会六点与我见面”。
The received packets of the message: 1 Mee 3 ea 6 clo 5 o' 4 t 6 2 tm 11 ion 7 ck 8 in 10 un 9 the 收到的消息包:1 Mee 3 ea 6 clo 5 o'4 t 6 2 tm 11 ion 7 ck 8 in 10 un 9 the
The received message is "Meet me at 6 o'clock in the union" 收到的消息是“在工会六点与我会面”
Why not sort the ArrayList
of Packets
itself, since you have implemented Comparable
interface? 由于已实现Comparable
接口,为什么不对Packets
的ArrayList
本身进行排序? You can 您可以
Collections.sort()
for this. 您可以为此使用Collections.sort()
。 nextPiece
to a String. 遍历排序后的列表,从列表中获取下一个项目,并将其nextPiece
附加到字符串中。 You should have the proper string after this. 之后,您应该具有正确的字符串。
Here I can give you some hints, but try to write the code yourself. 在这里,我可以给您一些提示,但是尝试自己编写代码。
First, use split(" ")
to split the String
1 Mee 3 ea 6 clo 5 o' 4 t 6 2 tm 11 ion 7 ck 8 in 10 un 9 the
into a String[]
. 首先,使用split(" ")
将String
1 Mee 3 ea 6 clo 5 o' 4 t 6 2 tm 11 ion 7 ck 8 in 10 un 9 the
拆分为String[]
。
Assume that numbers and string chunks appear alternately, put them into a Map<Integer, String>
. 假设数字和字符串块交替出现,将它们放入Map<Integer, String>
。 You may use a TreeMap
if you don't want further sorting. 如果您不想进一步排序,则可以使用TreeMap
。
Finally, iterate through the values in the Map
. 最后,遍历Map
的值。 Add them to a StringBuilder
and finally use toString()
to obtain a String
instance, which should be the result. 将它们添加到StringBuilder
,最后使用toString()
获得String
实例,应该是结果。
Edit: if Packet implements Comparable<Packet>
, a TreeSet
will do all the sorting for you (because there won't be any equal Packet
). 编辑:如果Packet implements Comparable<Packet>
, TreeSet
将为您进行所有排序(因为不会有任何相等的Packet
)。 See the following code (much simplified and condensed into a single java file for convenience). 请参阅以下代码(为方便起见,已大大简化并压缩到单个Java文件中)。
import java.util.*;
import java.io.FileNotFoundException; //for opening a file
import java.io.PrintWriter; //for writing to a file
public class Message
{
private String message;
private int packetLength;
private List<Packet> dividedMessage; //use java library java.util.List
private boolean messageSent;
private Random generator; //use java library java.util.Random
public Message(String theMessage, int thePacketLength)
{
message = theMessage;
packetLength = thePacketLength;
//receiveInFile(message);
}
public static void main(String[] args)
{
ArrayList<Packet> list = new ArrayList<>();
list.add(new Packet(1, "Mee"));
list.add(new Packet(3, "e a"));
list.add(new Packet(6, "clo"));
list.add(new Packet(5, "o\'"));
list.add(new Packet(4, "t 6 "));
list.add(new Packet(2, "t m"));
list.add(new Packet(11, "ion"));
list.add(new Packet(7, "ck "));
list.add(new Packet(8, "in "));
list.add(new Packet(10, "un"));
list.add(new Packet(9, "the "));
Collections.shuffle(list);
/*
* now shuffled
* read message
*/
TreeSet<Packet> set = new TreeSet<>(list);
StringBuilder builder = new StringBuilder();
for (Packet p: set)
{
builder.append(p.getMessage());
}
System.out.println(builder.toString());
}
public String toString()
{
return message;
}
static class Packet implements Comparable<Packet>
{
private int sequence; //sequence number of the packet
private String message; //a part of the message represented by the packet
public Packet(int sequenceNumber, String nextPiece) {
setSequence(sequenceNumber);
setMessage(nextPiece);
Message test = new Message(message, sequence);
}
@Override
public int compareTo(Packet o) {
int comparedSize = o.sequence;
if (this.sequence > comparedSize) {
return 1;
} else if (this.sequence == comparedSize) {
return 0;
} else {
return -1;
}
}
public String toString() {
return message;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public int getSequence() {
return sequence;
}
public void setSequence(int sequence) {
this.sequence = sequence;
}
}
}
IMO, a Set
is more convenient than a List
is there are no equal elements. IMO,如果没有相等的元素,则Set
比List
更方便。 Use a HashSet
if the order is unimportant, a LinkedHashSet
if the order must be unaltered, and a TreeSet
if natural ordering is required. 如果顺序不重要,则使用HashSet
如果顺序必须不变,则使用LinkedHashSet
如果需要自然顺序,则使用TreeSet
。
I tried the following code and it works fine: 我尝试了以下代码,它可以正常工作:
public static void main(String[] args) {
List<Packet> dividedMessage = new ArrayList<>();
dividedMessage.add(new Packet(1, "Mee"));
dividedMessage.add(new Packet(2, "t m"));
dividedMessage.add(new Packet(3, "e a"));
dividedMessage.add(new Packet(6, "clo"));
dividedMessage.add(new Packet(5, "o'"));
dividedMessage.add(new Packet(4, "t 6 "));
dividedMessage.add(new Packet(11, "ion"));
dividedMessage.add(new Packet(7, "ck"));
dividedMessage.add(new Packet(8, " in"));
dividedMessage.add(new Packet(10, " un"));
dividedMessage.add(new Packet(9, " the"));
Collections.sort(dividedMessage);
String originalMessage = "";
for (Packet packet : dividedMessage)
{
originalMessage += packet.getMessage();
}
System.out.println(originalMessage);
}
I hope this is what you want. 我希望这就是你想要的。 Output is: Meet me at 6 o'clock in the union
输出是: Meet me at 6 o'clock in the union
If you have lots of packets then performance may be a concern and you can use StringBuilder: 如果您有很多数据包,那么性能可能是一个问题,可以使用StringBuilder:
StringBuilder sb = new StringBuilder();
for (Packet packet : dividedMessage)
{
sb.append(packet.getMessage());
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.