2

We are supposed to put log entries (title and entry) in a sort of journal. That means having a list with each containing an array with room for two strings.

Now. We've worked with arrays and lists but never together.

Considering this is a school assignment I'm only asking for guidance to point me in the right direction.

So creating a list that on each entry contains space for an array of size 2. How should I think about this concept baring in mind I've only used them separately before?

3
  • I am not sure what you are confused about. It is a list where each element is an an array with enough size to hold two strings. Where is the confusion? Commented Jun 29, 2016 at 13:54
  • 10
    List<string[]> would be perfectly valid, although a List<Tuple<string, string>> or a List<LogEntry> where LogEntry is a class with two string properties would be more idiomatic. Note that there's not a declarative way to set the size of the underlying array. Commented Jun 29, 2016 at 13:55
  • Also, if you'd like you can just use a list nested in a list like List<List<string>>, though Stanley's answer is better in terms of design. If you need to use arrays and lists, his List<string[]> solution is the way to go. Commented Jun 29, 2016 at 14:02

6 Answers 6

6

A List<string[]> is a perfectly valid data structure, although a List<Tuple<string, string>> or a List<LogEntry> where LogEntry is a class with two string properties would be more idiomatic.

Note that there's not a declarative way to set the size of the underlying array (meaning that List<string[2]> is not a valid type.

Sign up to request clarification or add additional context in comments.

1 Comment

This way seems to fit this particular assignment the best. I've worked with classes before and since I could put methods inside this class as well it worked well for when I for instance want to print all the content of an entry as well as when need to search the.
2

Since you have to have a log title and entry, have you considered wrapping those in a separate class rather than using an array of two strings?

I mean doing something like this:

public class LogItem
{
    public string Item;
    public string Entry;
}

Then instead of having a list of arrays, you would just use a list of LogItem:

var myList = new List<LogItem>();

var myNewLogEntry = new LogItem {Title = "Entry title", Entry = "Entry message"};

myList.Add(myNewLogEntry);

1 Comment

Thanks! I couldn't figure out a problem for 2 days until I saw this! Awesome!
1

You can easily make list of arrays:

var myList = new List<string[]>();

myList.Add(new [] { "Fruit", "Orange" });
myList.Add(new [] { "Car", "Toyota" });

Console.WriteLine(myList[0][1]); // Prints "Orange"
Console.WriteLine(myList[1][0]); // Prints "Car"

Note, that array length for the particular item may differ. There's no way to limit it.

Comments

1

I'm not sure if this goes against the assignment, but I would suggest thinking a bit object oriented about this, consider what you're asking for:

We are supposed to do is put log entries (title and entry) in a sort of journal.

In my head this translates to two classes:

First a class "Log" with two string properties, "Title" and "Entry" Second a class "Journal" with a List<Log> property named Entries.

Code example:

public class Journal
{
    ...
    public List<Log> Entries { get; set; }
}

public class Log
{
    public string Title { get; set; }
    public string Entry { get; set; }
}

Comments

0

You can simply create it like that. Is that what you were looking for?

List<string[]> myList = new List<string[]>();

Comments

0

This is a completely valid data structure.

List<string[]> logList = new List<string[]>(); 
logList.Add(new string[] {"item 1", "item 2", "item 3"});

Besides this, I am not sure what you are looking for.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.