5

Few Days back, I faced a test where I was asked to answer the following question: Though it seems basic, I've some doubt and my own opinion

A publication center publishes books. A writer can write many books and a book has a author

There were four options and among them, I omitted two options that weren't close. So left two options as follows:

One Option With List:

public class Publisher
{
   public int PublisherId { get; set;}
   public string PublisherName { get; set;}
   public string Address { get; set;}
   public List<Author> Authors { get; set;}
   public List<Book> Books{ get; set;}
}   

public class Author
{
   public int AuthorId { get; set;}
   public string AuthorName { get; set;}
   public string AuthorAddress { get; set;}
   public List<Book> Books{ get; set;}
}   

public class Book
{
   public int BookId { get; set;}
   public string BookName { get; set;}
   public Author Author { get; set;}
} 

Another Option With Array:

public class Publisher
{
   public int PublisherId { get; set;}
   public string PublisherName { get; set;}
   public string Address { get; set;}
   public Author[] Authors { get; set;}
   public Book[] Books{ get; set;}
}   

public class Author
{
   public int AuthorId { get; set;}
   public string AuthorName { get; set;}
   public string AuthorAddress { get; set;}
   public Book[] Books{ get; set;}
}   

public class Book
{
   public int BookId { get; set;}
   public string BookName { get; set;}
   public Author Author { get; set;}
} 

In the same time, I looked into this link to understand about the difference: List Vs Array

Upon that, Firstly, I chose the first option (My answer was this) and believe, List has more functionality and would be a better choice. Another reason is that, I use EF in projects and when work with relationship with tables specifically for One-To-Many, then classes created, has a collection of List like this:

public List<Book> Books{ get; set;}

Secondly, I was thinking, if arrays could be used for the same, but what I learned array data structure is perfect for fixed data. I hope, I am on the right track.

Finally I was unable to understand two things from the link provided:

1) As a counter - List<T> is one-dimensional; where-as you have have rectangular (etc) arrays like int[,] or string[,,] - but there are other ways of modelling such data (if you need) in an object model

My views - One dimensional means is List a one-dimensional array or something related.

2) Little bit confused I mean in what case should we use the following with Array? Though it explains but need some more clarification:

  • it does a lot of bit-shifting, so a byte[] is pretty much essential for encoding;

  • I use a local rolling byte[] buffer which I fill before sending down to the underlying stream (and v.v.); quicker than BufferedStream etc;

  • it internally uses an array-based model of objects (Foo[] rather than List), since the size is fixed once built, and needs to be very fast.
user8512043
  • 1,043
  • 12
  • 20
  • Well, *personal opinion*: Apart from the array/Collection - question I think both soltuions are incorrect if taking the requirement apart word for word: There are two actors mentioned: "Writer" and "Author". The two may not be the same. In a Publishing-Evironment, knowing "ghost-writers" is not too far fetched, I think. But if these were the only reasonable choices left, then I think I may be taking it too far. – Fildor Aug 29 '17 at 08:35
  • I guess, **Author** and **Writer** are the same word. It seems confusing or given to confuse. – AT-2017 Aug 29 '17 at 08:59
  • Consider a book can has multiple authors (or writers), is the base relational logic constructed in right way? From what I think one-to-many relationship usually has `List` as reference to other tables. – Tetsuya Yamamoto Aug 29 '17 at 09:07
  • 1
    What is the question? You've given us a vague idea about a problem and detailed your investigation into Arrays and Lists but you don't actually ask a question. What do you want to know? – Jodrell Aug 29 '17 at 12:13
  • 1
    For what its worth, the test seems ill directed and vague but this is not test review exchange. – Jodrell Aug 29 '17 at 12:19

1 Answers1

1

As the link says first of all you have to understand that an array by default has a specific size which indicates how memory is allocated. If you exceed that allocation, you'll have to worry about allocating memory again to the whole array.

Lists are dynamic, which basically means that an element will only keep a pointer to the next element in line, so it's easy to add new items anywhere (front, back, middle). The main variable only keeps the pointer to the first element so that's how lists are being put together.

To be honest, in your example I'd use the lists version just because they can expand and contract on need basis, and it's more memory efficient.

On a side note, I'd rather use the IList or even IEnumerable type so you can leave open the concrete implementation to be used when the creator generates a new object.

You'd use IList when you need operations like Add, Remove and so on and you use IEnumerable when you only need to enumerate the elements (i.e. search, filter).

To answer your questions: 1) arrays are multi dimensional like int[,] which can easily be achieved with IList>; it's all abstract and I do recommend you study the linked list type (https://www.tutorialspoint.com/data_structures_algorithms/linked_list_algorithms.htm), maybe that'll answer a few questions 2) this is very specific to that application and usually some byte array operations will return byte arrays by default, again, because the size is predictable, so not sure what you're really looking for.

Hope this helps.

Andrei U
  • 1,908
  • 1
  • 11
  • 14
  • I do agree on it and thanks for the explanation. I would expect your answers for the next two questions at the end. – user8512043 Aug 29 '17 at 10:24
  • i've edited the answer, hope that'll clear some things for you – Andrei U Aug 29 '17 at 12:06
  • 1
    Actually, [`List`](https://referencesource.microsoft.com/#mscorlib/system/collections/generic/list.cs,2765070d40f47b98,references) uses an array internally, it's not a linked list. – Pieter Witvoet Aug 29 '17 at 12:23
  • So if `List` uses an array internally, then is `List` a one or multi-dimensional array I mean how the array is constructed @Pieter Witvoet? Just for clarification. – user8512043 Aug 30 '17 at 08:07
  • @user8512043: arrays *can* be multi-dimensional, but that's only useful in specific situations - it doesn't affect your use-case at all. – Pieter Witvoet Aug 30 '17 at 09:01