Java 在ArrayList中搜索<;整数>;在ArrayList内部并比较值
因此,我有一个Java 在ArrayList中搜索<;整数>;在ArrayList内部并比较值,java,arraylist,Java,Arraylist,因此,我有一个arrayList,它包含一个arrayList,其中包含如下索引值: [[0,1,6],[3,5,7],[10,11]] 我需要找到从一个到另一个的最短长度。在这种情况下,它将是6,7,10,长度将是3 问题是当并非所有的arrayList都是相同大小时,我不知道如何迭代。本质上,我需要遍历并比较每个索引和每个数组中的每个索引。所以在这种情况下会有3*3*2比较。我就是不知道如何在java中做到这一点 有人知道我如何在比较narrayList与其中不同数量的值时遍历它吗?虽然规
arrayList
,它包含一个arrayList
,其中包含如下索引值:
[[0,1,6],[3,5,7],[10,11]]
我需要找到从一个到另一个的最短长度。在这种情况下,它将是6,7,10
,长度将是3
问题是当并非所有的arrayList
都是相同大小时,我不知道如何迭代。本质上,我需要遍历并比较每个索引和每个数组中的每个索引。所以在这种情况下会有3*3*2
比较。我就是不知道如何在java中做到这一点
有人知道我如何在比较n
arrayList
与其中不同数量的值时遍历它吗?虽然规范不清楚,但我已经编写了这段代码-不是很优雅
static void findShortest( List<List<Integer>> listOfLists ){
int len = listOfLists.get(0).size();
List<List<Integer>> oldlop = new ArrayList<>(len);
List<Integer> olddist = new ArrayList<>(len);
for( int i = 0; i < len; ++i ){
oldlop.add( new ArrayList<Integer>() );
olddist.add( 0 );
}
for( int j = 1; j < listOfLists.size(); ++j ){
int newlen = listOfLists.get(j).size();
List<List<Integer>> newlop = new ArrayList<>(newlen);
List<Integer> newdist = new ArrayList<>(newlen);
step( oldlop, olddist,
listOfLists.get(j-1), listOfLists.get(j),
newlop, newdist );
oldlop = newlop;
olddist = newdist;
}
List<Integer> last = listOfLists.get( listOfLists.size()-1 );
for( int k = 0; k < last.size(); ++k ){
oldlop.get(k).add( last.get(k) );
}
System.out.println( oldlop );
System.out.println( olddist );
}
static void step( List<List<Integer>> oldlop,
List<Integer> olddist,
List<Integer> frlist,
List<Integer> tolist,
List<List<Integer>> newlop,
List<Integer> newdist ){
for( int ito = 0; ito < tolist.size(); ++ito ){
int min = Integer.MAX_VALUE;
int minind = 0;
List<Integer> minpath = null;
newlop.add( new ArrayList<Integer>() );
for( int ifr = 0; ifr < frlist.size(); ++ifr ){
int tst = olddist.get(ifr) +
Math.abs(tolist.get(ito)-frlist.get(ifr));
if( tst < min ){
min = tst;
minind = frlist.get(ifr);
minpath = oldlop.get(ifr);
}
}
newlop.get(ito).addAll( minpath );
newlop.get(ito).add( minind );
newdist.add( min );
}
}
输出是
[[6, 7, 10], [6, 7, 11]]
[4, 5]
最后一个列表中两个索引([10,11])的累积“长度”为[4,5]。第一个是最小值,这将选择收集的索引值的第一个列表:[6,7,10]。此代码为您提供最小路径长度。在这种情况下是13
import java.util.*;
import java.util.Collections;
public class HelloWorld{
public static void print(LinkedList<Integer> x){
for(int i = 0; i<x.size(); i++){
System.out.println(x.get(i));
}
}
public static int sum(LinkedList<Integer> x){
int acum = 0;
for(int i = 0; i<x.size(); i++){
acum+=x.get(i);
}
return acum;
}
public static LinkedList<Integer> permute(LinkedList<LinkedList<Integer>> array, int index, LinkedList<Integer> output, Integer flag, Integer aux, LinkedList<Integer> results){
print(output);
if(flag.equals(1)){
aux = array.size();
flag = 0;
}
if(output.size() == aux){
int x = sum(output);
results.add(x);
}
if(index == array.size()){
Collections.sort(results);
System.out.println("Final Shortest Path = " + results.get(0));
}
else{
for(int i=0 ; i<array.get(index).size(); i++){
output.add(array.get(index).get(i));
permute(array,index+1,output, flag, aux, results);
output.remove(output.size() - 1);
}
}
return output;
}
public static void main(String []args){
LinkedList<LinkedList<Integer>> x = new LinkedList<LinkedList<Integer>>();
LinkedList<Integer> z1 = new LinkedList<Integer>();
z1.add(0);
z1.add(1);
z1.add(6);
LinkedList<Integer> z2 = new LinkedList<Integer>();
z2.add(3);
z2.add(5);
z2.add(7);
LinkedList<Integer> z3 = new LinkedList<Integer>();
z3.add(10);
z3.add(11);
x.add(z1);
x.add(z2);
x.add(z3);
LinkedList<Integer> output = new LinkedList<Integer>();
LinkedList<Integer> results = new LinkedList<Integer>();
LinkedList<Integer> test = permute(x, 0, output, 1, 0, results);
print(test);
}
}
import java.util.*;
导入java.util.Collections;
公共类HelloWorld{
公共静态无效打印(LinkedList x){
对于(int i=0;iSorry,你能澄清一下你的要求吗?“从一个到另一个的最短长度”?我不确定你是从哪里得出这些数字的。我认为澄清你的问题会得到更好的答案,因为嵌套循环是你提出的XY问题的解决方案。
import java.util.*;
import java.util.Collections;
public class HelloWorld{
public static void print(LinkedList<Integer> x){
for(int i = 0; i<x.size(); i++){
System.out.println(x.get(i));
}
}
public static int sum(LinkedList<Integer> x){
int acum = 0;
for(int i = 0; i<x.size(); i++){
acum+=x.get(i);
}
return acum;
}
public static LinkedList<Integer> permute(LinkedList<LinkedList<Integer>> array, int index, LinkedList<Integer> output, Integer flag, Integer aux, LinkedList<Integer> results){
print(output);
if(flag.equals(1)){
aux = array.size();
flag = 0;
}
if(output.size() == aux){
int x = sum(output);
results.add(x);
}
if(index == array.size()){
Collections.sort(results);
System.out.println("Final Shortest Path = " + results.get(0));
}
else{
for(int i=0 ; i<array.get(index).size(); i++){
output.add(array.get(index).get(i));
permute(array,index+1,output, flag, aux, results);
output.remove(output.size() - 1);
}
}
return output;
}
public static void main(String []args){
LinkedList<LinkedList<Integer>> x = new LinkedList<LinkedList<Integer>>();
LinkedList<Integer> z1 = new LinkedList<Integer>();
z1.add(0);
z1.add(1);
z1.add(6);
LinkedList<Integer> z2 = new LinkedList<Integer>();
z2.add(3);
z2.add(5);
z2.add(7);
LinkedList<Integer> z3 = new LinkedList<Integer>();
z3.add(10);
z3.add(11);
x.add(z1);
x.add(z2);
x.add(z3);
LinkedList<Integer> output = new LinkedList<Integer>();
LinkedList<Integer> results = new LinkedList<Integer>();
LinkedList<Integer> test = permute(x, 0, output, 1, 0, results);
print(test);
}
}