해시 테이블을 기반으로하는 연관 배열 (사전이라고 함)을 사용해야합니다. 검색을위한 평균 시간 복잡성 - 최악의 경우 O (1 + n/k) 및 O (n). 표를 열 사전으로 구성해야합니다 (열 이름을 키로 사용). C#에서
http://en.wikipedia.org/wiki/Dictionary_(data_structure) http://en.wikipedia.org/wiki/Hash_table
예 :
using System;
using System.Collections;
using System.Collections.Generic;
namespace Test {
public class Test
{
class Table {
class Column {
public Dictionary<string, string> cells;
public Column() {
cells = new Dictionary<string, string>();
}
public string Find(string rowName) {
string resultValue;
if (cells.TryGetValue(rowName, out resultValue))
return resultValue;
else
throw new Exception("oops, no such cell");
}
}
Dictionary<string, Column> columns;
List<string> rowNames;
public Table() {
columns = new Dictionary<string, Column>();
rowNames = new List<string>();
}
public void AddColumn(string columnName, params string[] values) {
Column column = new Column();
columns.Add(columnName, column);
// fill new cells
int counter = 0;
foreach (string rowName in rowNames) {
if (counter < values.Length)
column.cells.Add(rowName, values[counter]);
else
column.cells.Add(rowName, "");
counter++;
}
}
public void AddRow(string rowName, params string[] values) {
rowNames.Add(rowName);
// fill new cells
int counter = 0;
foreach (KeyValuePair<string, Column> columnPair in columns) {
Column column = columnPair.Value;
if (counter < values.Length)
column.cells.Add(rowName, values[counter]);
else
column.cells.Add(rowName, "");
counter++;
}
}
public string Find(string columnName, string rowName) {
Column resultColumn;
if (columns.TryGetValue(columnName, out resultColumn))
return resultColumn.Find(rowName);
else
throw new Exception("oops, no such cell");
}
}
public static void Main()
{
Table table = new Table();
table.AddRow("a");
table.AddRow("b");
table.AddColumn("column 1", "asd", "asgf");
table.AddColumn("column 2", "awd", "aasf");
table.AddColumn("column 3", "asfc", "asgfc");
Console.WriteLine(table.Find("column 1", "b"));
}
}
}
완전히 데이터 당신에 따라 달라집니다 그리고 열은
상세 정보 (키와 행 이름) 값의 사전 있어야합니다 저장하려고합니다. 당신이하고 싶은 것에 대한 더 많은 정보를 제공해야합니다. – Config
당신은 무엇을 찾을 것입니까? –
이들은 문자열이됩니다 –