Java 从一个主阵列创建子阵列
我仍然掌握着Java。我需要一些在数组中循环的帮助 我的数组看起来像这样Java 从一个主阵列创建子阵列,java,arrays,slice,Java,Arrays,Slice,我仍然掌握着Java。我需要一些在数组中循环的帮助 我的数组看起来像这样 String [] allRecords = ["[BEGIN RECORD]", "[ID]1", "[cName]Agnes", "[Age]12", "[END RECORD]", "[BEGIN RECORD]", "[ID]2", "[cName]Hellen", "[Age]5", "[END RECORD]", "[BEGIN RECORD]", "[ID]3", "[cName]Jack", "[Age]3
String [] allRecords = ["[BEGIN RECORD]", "[ID]1", "[cName]Agnes", "[Age]12", "[END RECORD]", "[BEGIN RECORD]", "[ID]2", "[cName]Hellen", "[Age]5", "[END RECORD]", "[BEGIN RECORD]", "[ID]3", "[cName]Jack", "[Age]34", "[END RECORD]" ];
//这给了我一个新的子数组,如下所示:
“[ID]1”、“[cName]Agnes”、“[Age]12”
上面的代码工作正常。我现在需要的是从allRecords数组中读取/切片另一部分,即:“[ID]2”、“[cName]Hellen”、“[Age]5”,然后切片下一个区块“[ID]3”、“[cName]Jack”、“[Age]34”,直到allRecords数组结束
我该怎么做
谢谢大家! 您现有的代码非常接近,可以很容易地进行修改以实现您的目标。要记住的关键一点是,您现在没有这样做,即从停止的地方开始,而不是在0处重新启动。因此,您已经(为了便于说明而大大简化了):
int foundIndex=0;
首先,感谢Trenin和Jason的指导。
我努力完成这项任务,总有一天为了别人的利益,我会把为我工作的代码贴在下面
String [] allRecords = {"[BEGIN RECORD]", "[ID]1", "[cName]Agnes", "[Age]12", "[END RECORD]", "[BEGIN RECORD]", "[ID]2", "[cName]Hellen", "[Age]5", "[END RECORD]", "[BEGIN RECORD]", "[ID]3", "[cName]Jack", "[Age]34", "[END RECORD]"};
String beginRecord = "[BEGIN RECORD]";
String endRecord = "[END RECORD]";
int foundIndex = 0;
int foundEnd = 0;
for (int i=0; i<allRecords.length; i++) {
if (endRecord.equals(allRecords[i])) {
foundEnd = i;
break;
}
}
//by saving the location of the end of the previous record, and picking up from there, your logic can now be repeatedly in a loop until all valid records are consumed from the input
foundEnd = foundEnd-1; //arrays are zero based
for (int i=0; i<allRecords.length; i++) {
if (beginRecord.equals(allRecords[i])) {
foundIndex = i+1; //arrays are zero based
String [] partAllRecords = Arrays.copyOfRange(allRecords, foundIndex, foundIndex+foundEnd);
System.out.println(Arrays.toString(partAllRecords));
//prints below arrays in logcat
//[[ID]1, [cName]Agnes, [Age]12]
//[[ID]2, [cName]Hellen, [Age]5]
//[[ID]3, [cName]Jack, [Age]34]
}
}
String[]allRecords={“[BEGIN RECORD]”、“[ID]1”、“[cName]Agnes”、“[Age]12”、“[END RECORD]”、“[BEGIN RECORD]”、“[ID]2”、“[cName]Hellen”、“[Age]5”、“[END RECORD]”、“[ID]3”、“[cName]Jack”、“[Age]34”、“[END RECORD]”;
字符串beginRecord=“[BEGIN RECORD]”;
字符串endRecord=“[END RECORD]”;
int foundIndex=0;
int foundEnd=0;
对于(int i=0;iYou可以添加循环以多次重复此操作。
//i then use the below code to slice off part of the array
String [] partAllRecords = Arrays.copyOfRange(allRecords, foundIndex, foundEnd);
int foundIndex = 0;
for (int i=0; i<allRecords.length; i++)
... find start record
int foundEnd = 0;
for (int i=0; i<allRecords.length; i++) {
... find end record
int foundIndex, foundEnd = -1;
do {
foundIndex = 0;
for (int i=foundEnd + 1; i<allRecords.length; i++)
... find start record
foundEnd = 0;
for (int i=foundIndex + 1; i<allRecords.length; i++) {
... find end record
} while a record was found;
String [] allRecords = {"[BEGIN RECORD]", "[ID]1", "[cName]Agnes", "[Age]12", "[END RECORD]", "[BEGIN RECORD]", "[ID]2", "[cName]Hellen", "[Age]5", "[END RECORD]", "[BEGIN RECORD]", "[ID]3", "[cName]Jack", "[Age]34", "[END RECORD]"};
String beginRecord = "[BEGIN RECORD]";
String endRecord = "[END RECORD]";
int foundIndex = 0;
int foundEnd = 0;
for (int i=0; i<allRecords.length; i++) {
if (endRecord.equals(allRecords[i])) {
foundEnd = i;
break;
}
}
//by saving the location of the end of the previous record, and picking up from there, your logic can now be repeatedly in a loop until all valid records are consumed from the input
foundEnd = foundEnd-1; //arrays are zero based
for (int i=0; i<allRecords.length; i++) {
if (beginRecord.equals(allRecords[i])) {
foundIndex = i+1; //arrays are zero based
String [] partAllRecords = Arrays.copyOfRange(allRecords, foundIndex, foundIndex+foundEnd);
System.out.println(Arrays.toString(partAllRecords));
//prints below arrays in logcat
//[[ID]1, [cName]Agnes, [Age]12]
//[[ID]2, [cName]Hellen, [Age]5]
//[[ID]3, [cName]Jack, [Age]34]
}
}