Ich muss einen Validator schreiben, der nach Räumen sucht, die von einem JSON mit Gson analysiert wurden, das für jedes Zimmerpaar A und B, wenn Sie von A nach B gelangen können Sie können hier das Format für die JSON von B nach A.Effiziente Methode, um zu überprüfen, ob eine Menge symmetrisch ist
erhalten: https://jsfiddle.net/tgtbqzky/
{
"initialRoom": "MatthewsStreet",
"rooms": [
{
"name": "MatthewsStreet",
"description": "You are on Matthews, outside the Siebel Center",
"directions": [
{
"direction": "East",
"room": "SiebelEntry"
}
]
},
{
"name": "SiebelEntry",
"description": "You are in the west entry of Siebel Center. You can see the elevator, the ACM office, and hallways to the north and east.",
"directions": [
{
"direction": "West",
"room": "MatthewsStreet"
},
{
"direction": "Northeast",
"room": "AcmOffice"
},
{
"direction": "North",
"room": "SiebelNorthHallway"
},
{
"direction": "East",
"room": "SiebelEastHallway"
}
]
},
{
"name": "AcmOffice",
"description": "You are in the ACM office. There are lots of friendly ACM people.",
"directions": [
{
"direction": "South",
"room": "SiebelEntry"
}
]
},
{
"name": "SiebelNorthHallway",
"description": "You are in the north hallway. You can see Siebel 1112 and the door toward NCSA.",
"directions": [
{
"direction": "South",
"room": "SiebelEntry"
},
{
"direction": "NorthEast",
"room": "Siebel1112"
}
]
},
{
"name": "Siebel1112",
"description": "You are in Siebel 1112. There is space for two code reviews in this room.",
"directions": [
{
"direction": "West",
"room": "SiebelNorthHallway"
}
]
},
{
"name": "SiebelEastHallway",
"description": "You are in the east hallway. You can see Einstein Bros' Bagels and a stairway.",
"directions": [
{
"direction": "West",
"room": "SiebelEntry"
},
{
"direction": "South",
"room": "Siebel1314"
},
{
"direction": "Down",
"room": "SiebelBasement"
}
]
},
{
"name": "Siebel1314",
"description": "You are in Siebel 1314. There are happy CS 126 students doing a code review.",
"directions": [
{
"direction": "North",
"room": "SiebelEastHallway"
}
]
},
{
"name": "SiebelBasement",
"description": "You are in the basement of Siebel. You see tables with students working and door to computer labs.",
"directions": [
{
"direction": "Up",
"room": "SiebelEastHallway"
}
]
}
]
}
ich mich gefragt, ob die Art und Weise zwei verschachtelte for-Schleifen zu gehen wäre, wobei die äußere for-Schleife würde Schleife durch alle Räume würde der innere durch jede mögliche Richtung in jedem Loop schleifen, und dann würde ich jedes Paar hinzufügen, das ich in einen Arr bekomme aylist.
Wenn ich auf etwas stoße, das bereits vorhanden ist, würde ich es aus der ArrayList entfernen, und wenn am Ende meiner for-Schleifen die ArrayList immer noch ein Element enthält, ist das entsprechende Paar nicht vorhanden, und Daher ist der JSON ungültig. Wenn die Größe der ArrayList Null ist, sind die Daten gültig.
Hat jemand eine effizientere Methode, dieses Problem zu lösen? Ich habe das Gefühl, da es im Wesentlichen beweist, ob eine gegebene Menge symmetrisch ist, muss es eine optimierte Methode geben.
Sie haben einen Code zur Deserialisierung der JSON zu einer Klasse richtig? – UmNyobe
Ja, ich habe alle Klassen und Funktionen. – franklinsing