d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Objects And Arrays
Add Reply New Topic New Poll
Member
Posts: 16,404
Joined: Mar 28 2009
Gold: 7.69
Jul 11 2015 09:07am
Can someone explain, in simple terms, what the purpose of objects and arrays are? What is a real life example where objects and arrays would be used?

I've been doings objects/arrays all morning (javascript). I can do them but I dont understand WHY do them?

This post was edited by Shakti on Jul 11 2015 09:16am
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Jul 11 2015 09:20am
Quote (Shakti @ Jul 11 2015 10:07am)
Can someone explain, in simple terms, what the purpose of objects and arrays are? What is a real life example where objects and arrays would be used?

I've been doings objects/arrays all morning. I can do them but I dont understand WHY do them?


Objects - I want a single structure which represents something meaningful to me (more meaningful than say, an integer, which stores a signed/unsigned number)

Arrays - I want to store more than 1 thing inside a single thing where the order is either unnecessary (because maybe we are 'consuming' the things) or the order is known by the developer

so instead of:

Code
Int a, b, c, d , e, f, g, h, i, j, k, l, m , n, o, p, q, r, s, t, u, v, w, x, y, z;



I can just say

Code
myIntArray a[25];



I haven't even talked about queue/stack/BST etc with arrays either.

Or all the crazy shit with objects.

Feel free to look online for enlightenment.

This post was edited by Eep on Jul 11 2015 09:21am
Member
Posts: 16,404
Joined: Mar 28 2009
Gold: 7.69
Jul 11 2015 09:23am
Quote (Eep @ Jul 11 2015 10:20am)
Objects - I want a single structure which represents something meaningful to me (more meaningful than say, an integer, which stores a signed/unsigned number)

Arrays - I want to store more than 1 thing inside a single thing where the order is either unnecessary (because maybe we are 'consuming' the things) or the order is known by the developer

so instead of:

Code
Int a, b, c, d , e, f, g, h, i, j, k, l, m , n, o, p, q, r, s, t, u, v, w, x, y, z;



I can just say

Code
myIntArray a[25];



I haven't even talked about queue/stack/BST etc with arrays either.

Or all the crazy shit with objects.

Feel free to look online for enlightenment.


thanks :)

that scares me lol
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Jul 11 2015 09:28am
Quote (Shakti @ Jul 11 2015 10:23am)
thanks :)

that scares me lol


it shouldn't

just remember - you don't really need to learn about something until you have a reason to use it.


That is why a lot of people can get by pretty far without a college education in CS.

If you need to do something, and it turns out there is a concept/idea for that, you look it up, THEN you learn it. (And typically, I would say things learned this way tend to retain better than those learned purely in academia)
Member
Posts: 16,404
Joined: Mar 28 2009
Gold: 7.69
Jul 11 2015 09:37am
Quote (Eep @ Jul 11 2015 10:28am)
it shouldn't

just remember - you don't really need to learn about something until you have a reason to use it.


That is why a lot of people can get by pretty far without a college education in CS.

If you need to do something, and it turns out there is a concept/idea for that, you look it up, THEN you learn it. (And typically, I would say things learned this way tend to retain better than those learned purely in academia)


i completely agree. I just started learning my first language, js, and despite the headaches it causes i enjoy doing it and trying to solve problems associated with it.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Jul 11 2015 11:56am
lets take a look at an example.


without getting too technical into the details, conceptually, how would you build this?
I would probably split it into 3 sections: main controller, person entity, and ui-row.

controller's responsibility is to handle the main page logic. it doesn't need to know any details about the data. it just needs to fetch some list and bind it to the screen.

ex:
Code
function init(){
var peopleToShow = getPeopleToShow(); // magically function that gets our data as an array
peopleToShow.forEach(function(person){
_uiList.add(new UIRow(person));
});
}


now your ui row looks something like this. given a person, it just displays the data
Code
<Row>
<Image src="{person.image}"/>
<Label text="{person.title}"/><Label text="{current_date - person.last_status_update}"/>
<Label text="{person.status}"/>
<Label text="{person.city}, {person.state}"/>
</Row>


and your person object looks something like this. it holds all the metadata for a single person
Code
var person = {
image: "www.images.com/blah.png",
title: "Mom",
status: "Home Sweet Home",
city: "New York",
state: "NY",
last_status_update: 6/2/2013 3pm
};


how would you do this without arrays and objects?

i just used a data transfer object, meaning it only holds data. keep in mind objects can also perform actions and in other languages you can use a lot of other features. you can have classes define their structure and inherit functionality from other classes.

This post was edited by carteblanche on Jul 11 2015 11:58am
Member
Posts: 16,404
Joined: Mar 28 2009
Gold: 7.69
Jul 11 2015 02:03pm
Quote (carteblanche @ Jul 11 2015 12:56pm)
lets take a look at an example.
http://www.codelearn.org/android-tutorial/assets/list_view/list-view-example-1-3928c884e90efde573574bee3a89e868.jpg

without getting too technical into the details, conceptually, how would you build this?
I would probably split it into 3 sections: main controller, person entity, and ui-row.

controller's responsibility is to handle the main page logic. it doesn't need to know any details about the data. it just needs to fetch some list and bind it to the screen.

ex:
Code
function init(){
var peopleToShow = getPeopleToShow(); // magically function that gets our data as an array
peopleToShow.forEach(function(person){
_uiList.add(new UIRow(person));
});
}


now your ui row looks something like this. given a person, it just displays the data
Code
<Row>
<Image src="{person.image}"/>
<Label text="{person.title}"/><Label text="{current_date - person.last_status_update}"/>
<Label text="{person.status}"/>
<Label text="{person.city}, {person.state}"/>
</Row>


and your person object looks something like this. it holds all the metadata for a single person
Code
var person = {
image: "www.images.com/blah.png",
title: "Mom",
status: "Home Sweet Home",
city: "New York",
state: "NY",
last_status_update: 6/2/2013 3pm
};


how would you do this without arrays and objects?

i just used a data transfer object, meaning it only holds data. keep in mind objects can also perform actions and in other languages you can use a lot of other features. you can have classes define their structure and inherit functionality from other classes.


ok that makes sense! O_O.. so much to grasp. But i see how html/js intertwine
Member
Posts: 5,167
Joined: Nov 23 2006
Gold: 11.01
Jul 21 2015 09:37am
Objects make life so much easier. Instead of using a manually maintained linkedlist by moving the pointers etc you can just use a Collections remove() function and all the work is done for you. That's more of a procedural language example though.
Member
Posts: 24,488
Joined: Jul 11 2011
Gold: 1,272.50
Aug 17 2015 04:19am
Quote (Eep @ 11 Jul 2015 08:20)
Objects - I want a single structure which represents something meaningful to me (more meaningful than say, an integer, which stores a signed/unsigned number)

Arrays - I want to store more than 1 thing inside a single thing where the order is either unnecessary (because maybe we are 'consuming' the things) or the order is known by the developer

so instead of:

Code
Int a, b, c, d , e, f, g, h, i, j, k, l, m , n, o, p, q, r, s, t, u, v, w, x, y, z;



I can just say

Code
myIntArray a[25];



I haven't even talked about queue/stack/BST etc with arrays either.

Or all the crazy shit with objects.

Feel free to look online for enlightenment.


I'm sorry, but this is not even valid JavaScript. And bracket notation grabs the property name from an object to get its value, or the character inside a string... No idea where you got 25 from.

This post was edited by HighschoolTurd on Aug 17 2015 04:24am
Member
Posts: 29,723
Joined: Jun 11 2007
Gold: 279.52
Aug 21 2015 08:56am
Simple think of an object that is one entity but can have multiple set of properties. These properties can be strings, numbers even functions.

An array is basically a variable that contains multiple values
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll