nus-cs2030 / 2021-s2

2 stars 2 forks source link

Immutable list for other primitives #72

Open wentinggy opened 3 years ago

wentinggy commented 3 years ago

Description

During lecture 7, prof explained that to define an immutable list for other primitives/objects we can copy the entire class, change int to double. But I'm still unsure of how that works. Does anyone have any idea how the code should look like/ what does changing int to double exactly mean? (slide 3/16) Thankyou!

Screenshots

Screenshot 2021-03-14 at 6 53 45 PM
Keithlimhs commented 3 years ago

Hello basically in that slide he showed an example of defining an immutable list using int array. To define an immutable list for other primitives/objects he meant that we can copy everything in the class and just change all the int to whatever primitives we want, for e.g. double[] array

jiajun-seah commented 3 years ago

You could potentially create different ImLists for double, boolean etc. but the point of this question in the slide is to make you realise that doing so would be violating the abstraction principle (there are commonalities between all these lists - they have the 'structure' of an ImList after all). These ImLists of different primitives/objects can have common methods, so we're trying to create a generic ImList that can apply to all types instead.

gableejh commented 3 years ago

For example, if the initial immutable list is written for integer type, you can only use the codes for integer and if you would like to use it for other types such as string, double, boolean, object etc., it's something like using that initial code that you wrote but changing the type to what you would like

E.g. instead of List = new ArrayList(); just create a new class with the same codes but update the type to - List = new ArrayList();

ashleyleerx commented 3 years ago

Hi as what many others have mentioned the prof used int[] array as an example. You can change all the int to double for instance but that would result in unnecessary duplication of code so it is best to use generics by using

wentinggy commented 3 years ago

thankyou everyone!