When to use List <Long> instead of long []?
There is something I really don’t understand: lots (see my comment) of people complain that Java is not OO, because you still have access to primitive and primitive arrays. Some people say they should leave ...
However, I do not understand ... Could you effectively perform functions such as signal processing (for example, write FFT, for starters), write effective encryption algorithms, write fast image manipulation libraries, etc. in Java, if you have t access to, say, int [] and long []?
Should I start writing my Java software using List <Long> instead of long []?
If the answer is “just use higher-level libraries that do what you need” (for example, say, signal processing), then how should these libraries be written?
I personally use List in most cases because it gives you a lot of convenience. You can also have concurrent collections, but not parallel source arrays.
Almost the only situation when I use raw arrays is when I read a large fragment of binary data, such as image processing. I am interested in instantiating, for example, Byte objects 100M times, although I have to admit that I have never tried working with this huge Byte list. I noticed that you have something like a 100 KB file, the List<Byte> working fine.
Most examples of image processing, etc. also use an array, so it is actually more convenient to use raw arrays in this field.
So, in conclusion, my practical answer to this is:
Use wrappers if you don’t
- Work with a very large array as long> 10 m (I'm too lazy to write a landmark!),
- Work in a field where many examples or people prefer unprocessed arrays (for example, network programming, image processing),
- You have learned that there is significant performance by switching to raw arrays while doing experiments.
- If for some reason it is more reasonable to work with raw arrays on this problem for you.
In high-performance computing, arrays of objects (as well as primitives) are necessary because they are more reliably mapped to the underlying CPU architecture and behave more predictably for things like cache access and garbage collection. With such methods, Java is very successfully used in areas where the wisdom gained is that the language is not suitable.
However, if your goal is solely to write code that is very well supported and seems to be self-consistent, then more rigorous constructions are the obvious way. In your direct comparison, the List object hides the problem of memory allocation, increases your list, etc., and also provides (in various implementations) additional tools, such as certain access patterns, such as stacks or queues. Using generics also allows for refactoring with a much greater degree of confidence and full support for your IDE and toolchain.
An enlightened developer must make the appropriate choice for the use case they are approaching. By suggesting that the language is not enough "OO" because it allows such a choice, I can suspect that the person either does not believe that other developers are as smart as they are, or did not have particularly wide experience in different areas of applications.
Good.
You need to know the size of the array at the time of its creation, but you cannot change its size after its creation. But, after it was created, the list can grow dynamically, and for this there is a function .Add() .
Did you go through this link?
Good comparison of arrays and list.