实现一
做路径分析的时候要实现如下这样的功能,简单实现一下....性能有待优化
有如下页面:"P1","P2","P3","P4","P5","P6","P4"
要对每一个页面获取前/后四级页面,没有的置为-1,得到如下的结果:
[-1, -1, -1, -1, P1, P2, P3, P4, P5]
[-1, -1, -1, P1, P2, P3, P4, P5, P6]
[-1, -1, P1, P2, P3, P4, P5, P6, P4]
[-1, P1, P2, P3, P4, P5, P6, P4, -1]
[P1, P2, P3, P4, P5, P6, P4, -1, -1]
[P2, P3, P4, P5, P6, P4, -1, -1, -1]
[P3, P4, P5, P6, P4, -1, -1, -1, -1]
import java.util.Arrays;
public class demo2 {
public static void main(String[] args) {
String[] st = {"P1","P2","P3","P4","P5","P6","P4"};
String[] st1 = {"-1","-1","-1","-1","-1","-1","-1","-1","-1"};
for (int i = 0; i < st.length; i++) {
String[] clone = st1.clone();
for (int i1 = Math.max(i-4,0); i1 <= Math.min(i+4,st.length ); i1++) {
if(i1 < st.length) { //超过数组长度 st[i1] 就越界了
clone[i1 + 4 - i] = st[i1];
}
}
System.out.println(Arrays.toString(clone));
}
}
}
网友评论