我有Double
个值,我使用floor()
将0转为1,并将其他双值转换为Integer
。然后,我使用contains()
检查值是否存在,如果不存在,则将它们附加到数组中。这是片段:
let radius = center.distanceFromLocation(location) / 1000
let section = floor(radius) == 0 ? 1 : Int(radius)
if !self.myArray.contains(Int(radius)) {
self.myArray.append(section)
}
问题是,它在myArray
中为“1”和“1从0舍入”附加了不同的值。
以下是一些调试:
(lldb) po myArray
▿ 3 elements
- [0] : 1
- [1] : 1 { ... }
- [2] : 11 // it's working fine for other numbers
(lldb) po myArray[0]
1
(lldb) po myArray[1]
1
(lldb) po myArray[2]
11
(lldb) p myArray[0]
(Int) $R11 = 1
(lldb) p myArray[1]
(Int) $R10 = 1
答案 0 :(得分:1)
您正在检查!self.myArray.contains(Int(radius))
哪种情况有误。
假设数组已经包含1
,您知道是否想要插入0.3
。 section
变为1
,但是您是否已经检查Int(radius)
= 0
已经包含section
= 1
。然后再次添加if !self.myArray.contains(section) {
self.myArray.append(section)
}
(Set
)。
您的代码应为
ack -f --cpp --nohh
或者使用import java.util.ArrayList;
import java.util.List;
import com.orientechnologies.orient.client.remote.OServerAdmin;
import com.tinkerpop.blueprints.Direction;
import com.tinkerpop.blueprints.Edge;
import com.tinkerpop.blueprints.Vertex;
import com.tinkerpop.blueprints.impls.orient.OrientGraph;
public class myClass {
private boolean stop=false;
private List<Vertex> visitedNodesPreviousStep=new ArrayList<Vertex>();
private List<Vertex> visitedNodeCurrently=new ArrayList<Vertex>();
private List<List<Vertex>> path_vertex=new ArrayList<List<Vertex>>();
private List<List<Edge>> path_edges=new ArrayList<List<Edge>>();
private OrientGraph g;
int step=0;
public myClass(OrientGraph g) {
this.g=g;
}
protected List<Object> getDistance(String starting_rid, String ending_rid) {
Vertex starting_node=g.getVertex(starting_rid);
Vertex ending_node=g.getVertex(ending_rid);
visitedNodesPreviousStep.add(starting_node);
List<Vertex> p1=new ArrayList<Vertex>();
p1.add(starting_node);
path_vertex.add(p1);
step=1;
boolean found_node_to_be_added=false;
do{
stop=false;
found_node_to_be_added=false;
for(Vertex v: visitedNodesPreviousStep){
List<Edge> edges_to_be_added=new ArrayList<Edge>();
List<Vertex> nodes_to_be_added=new ArrayList<Vertex>();
Iterable<Edge> it_edge = (Iterable<Edge>) v.getEdges(Direction.OUT);
for(Edge e1:it_edge){
Vertex v1=e1.getVertex(Direction.IN);
edges_to_be_added.add(e1);
nodes_to_be_added.add(v1);
String rid=v1.getId().toString();
if(!rid.equals(ending_rid)){ // checking the current @rid isn't the ending
visitedNodeCurrently.add(v1);
}
else{ // ending node found
setPathFoundList(v,ending_node,step,e1);
stop=true;
}
}
if(nodes_to_be_added.size()!=0 && stop==false){
found_node_to_be_added=true;
setpath_vertex(v,nodes_to_be_added,edges_to_be_added);
}
}
if(found_node_to_be_added==false){
stop=true;
}
System.out.println("step = " + step + " " +path_vertex);
change();
step++;
}while(stop==false);
clean_vertex_path(ending_node);
return getShortestPathList();
}
public void change(){
visitedNodesPreviousStep.clear();
for(Vertex v:visitedNodeCurrently)
visitedNodesPreviousStep.add(v);
visitedNodeCurrently.clear();
}
private void setPathFoundList(Vertex node,Vertex ending_node,int step,Edge edge){
for(int i=0;i<path_vertex.size();i++){
List<Vertex> path=path_vertex.get(i);
Vertex last=path.get(path.size()-1);
if(last.getId().equals(node.getId()) && path.size()==step){
path.add(ending_node);
List<Edge> edgesPath=path_edges.get(i);
edgesPath.add(edge);
}
}
}
private void setpath_vertex(Vertex node,List<Vertex> nodes_to_be_added,List<Edge> edges_to_be_added) {
for(int i=0;i<path_vertex.size();i++){
List<Vertex> path=path_vertex.get(i);
Vertex last=path.get(path.size()-1);
if(last.getId().equals(node.getId())){
int j=0;
for(int h=0;h<nodes_to_be_added.size();h++){
boolean name_present=false;
for(Vertex p:path){
if(p.getId().equals(nodes_to_be_added.get(h).getId()))
name_present=true;
}
if(name_present==false){
List<Vertex> p2=new ArrayList<Vertex>();
for(Vertex p:path)
p2.add(p);
p2.add(nodes_to_be_added.get(h));
List<Edge> e2=new ArrayList<Edge>();
if(step==1){
e2.add(edges_to_be_added.get(h));
}
else{
List<Edge> edgesPath=path_edges.get(i);
for(Edge p1:edgesPath)
e2.add(p1);
e2.add(edges_to_be_added.get(h));
}
if(j==0){
path_vertex.set(i, p2);
if(step==1){
path_edges.add(i, e2);
}
else{
path_edges.set(i, e2);
}
j++;
}
else{
path_vertex.add(p2);
path_edges.add(e2);
}
}
}
}
}
}
public void clean_vertex_path(Vertex ending_node_name){
for(int i=0;i<path_vertex.size();i++){
List<Vertex> path=path_vertex.get(i);
if(!path.get(path.size()-1).getId().equals(ending_node_name.getId())){
path_vertex.remove(i);
path_edges.remove(i);
i--;
}
}
}
public List<Object> getShortestPathList(){
List<Object> result=new ArrayList<Object>();
if(path_vertex.size()==0)
return new ArrayList<Object>();
else{
List<Vertex> path=path_vertex.get(0);
int min_size= path.size();
for(int i=0;i<path_vertex.size();i++){
if(path_vertex.get(i).size()<=min_size){
List<Vertex> path2= path_vertex.get(i);
List<Edge> edges2= path_edges.get(i);
for(int k=0;k<path2.size();k++){
result.add(path2.get(k));
if(k!=path2.size()-1)
result.add(edges2.get(k));
}
}
}
}
return result;
}
public static void main(String[] args) {
String remote="remote:localhost/";
String DBname="Stack36794694";
String currentPath=remote+DBname;
OServerAdmin serverAdmin;
try {
serverAdmin = new OServerAdmin(currentPath).connect("root", "root");
if(serverAdmin.existsDatabase()){
OrientGraph g=new OrientGraph(currentPath);
myClass shortest2 = new myClass(g);
System.out.println("SHORTEST PATH " + shortest2.getDistance("#9:0","#9:5"));
}
}
catch(Exception e){
}
}
}
确保每个值最多包含一次。
答案 1 :(得分:0)
是的,因为您获取Int(1)和Int(0.45)不同的Int(半径)。 您应该尝试检查myArray是否包含部分,而不是Int(半径),因为它可能会导致您遇到的行为不正确。