Vs Array List: One-to-Many and Related

A few days ago I ran into a test when I was asked to answer the following question: although this seems to be the main one, I have some doubts and my own opinion.

Publishing Center publishes books. A writer can write many books, and a book has an author

There were four options, and among them, I omitted two options that were not close. Thus, two options remain:

One option with a 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 an 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;} } 

At the same time, I looked at this link to understand the difference: Vs Array List

After that, firstly, I chose the first option (my answer was that) and believe, List has more functionality and will be a better choice. Another reason is that I use EF in projects and when working with table relationships specifically for One-to-Many , then the created classes have a List collection as follows:

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

Secondly, I thought: if arrays can be used for the same thing, but what I learned is that the data structure of the array is ideal for fixed data. I hope I'm on the right track.

Finally, I could not understand two things from the provided link :

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

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

2) A little embarrassed I mean, in which case should we use the following with Array ? Although this explains, it needs further clarification:

  • it performs a lot of bit-shift, so byte [] is very important for encoding;

  • I use the local byte buffer [], which I fill before sending to the base stream (and vv); faster than BufferedStream etc .;

  • it internally uses an array-based object model (Foo [], not List), since the size is fixed after creation and should be very fast.
+5
source share
1 answer

As the link says, first of all you need to understand that the array by default has a certain size, which indicates how memory is allocated. If you exceed this allocation, you will have to worry about allocating memory for the entire array again.

Lists are dynamic, which basically means that the element will contain a pointer to the next element in the line, so it’s easy to add new elements anywhere (front, back, in the middle). The main variable holds the pointer only on the first element, so that the list of lists is combined.

Honestly, in your example, I would use the version of lists only because they can expand and shrink as needed, and this is more memory efficient.

On the side of the note, I would prefer to use the IList type or even IEnumerable, so that you can leave open the concrete implementation that will be used when the creator creates a new object.

You should use IList when you need operations such as Add, Delete, etc., and you use IEnumerable when you only need to list items (like search, filter).

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

Hope this helps.

+1
source

Source: https://habr.com/ru/post/1271319/


All Articles