下面的代码是 Android 应用程序活动的一部分。我正在使用 Jena(Androjena) 来查询 rdf 文件。那部分工作正常。问题是我试图遍历结果并将其存储在二维数组中。列和行应该是这样的:
/////////////////
Adam /Sandler/43
Kate /Sandler/22
Mike /Jasonss/13
/////////////////
代码:
enter code here
// Query uses an external SPARQL endpoint for processing
// This is the syntax for that type of query
QueryExecution qe = QueryExecutionFactory.sparqlService(sparqlEndpointUri, query);
// Execute the query and obtain results
ResultSet resultSet = qe.execSelect();
// Setup a place to house results for output
StringBuffer results = new StringBuffer();
// Get the column names (the aliases supplied in the SELECT clause)
List<String> columnNames = resultSet.getResultVars();
int i=0;
int j=0;
String results1[][] = new String[i][j];
// Iterate through all resulting rows
while (resultSet.hasNext()) {
// Get the next result row
QuerySolution solution = resultSet.next();
results1 = new String[i][columnNames.size()];
// Iterate through the columns
for (String var : columnNames) {
// Add the column label to the StringBuffer
results.append(var + ": ");
// Add the returned row/column data to the StringBuffer
// Data value will be null if optional and not present
if (solution.get(var) == null) {
results.append("{null}");
// Test whether the returned value is a literal value
} else if (solution.get(var).isLiteral()) {
results.append(solution.getLiteral(var).toString());
results1[resultSet.getRowNumber()][j]=solution.getLiteral(var).toString();
j++;
// Otherwise the returned value is a URI
} else {
results.append(solution.getResource(var).getURI().toString());
results1[resultSet.getRowNumber()][j]=solution.getResource(var).getURI().toString();
j++;
}
results.append('\n');
}
results.append("-----------------\n");
i++;
}
PS。忽略字符串缓冲区。