Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List Vs Array: One-To-Many and Has-A Relationship

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.
like image 357
user8512043 Avatar asked Nov 07 '22 17:11

user8512043


1 Answers

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.

like image 74
Andrei U Avatar answered Nov 14 '22 22:11

Andrei U