Arrays enable the representation of a number of similar items (in terms of their
ID: 3814261 • Letter: A
Question
Arrays enable the representation of a number of similar items (in terms of their datatypes). They represent these items in an ordered list. Consider the features of arrays and respond to the following:
•In what programming situations would the use of an array be beneficial?
•What situations would not warrant the use of an array? Provide an example explaining why?
•Imagine a programming structure that would deal with these problems. What characteristics should the structure possess?
•Research the concept of class ArrayList in Java. Describe the advantage of using the ArrayList class over an array. Also discuss in what circumstances you should use one over the other. Please use examples to justify your answers.
Explanation / Answer
1)Arrays are data structure which is used for storing similar type of elements..
Arrays are used when u have fixed set of elements..
i cannot be resizeable...
so it is used when you have fixed set of similar elements..
the size of memory used by arrays is relatively small compare to arraylist..
the time taken to acces elements in array is faster than compare to access elements in arraylist..
converting from int to float or float to int or long to int is very significant in arrays.....
2)before going to where we wont use array..instead we use arraylist..we first should know what is arraylist
arrays have minimum of 105 methods..
arraylist is a class that implements list interface..arraylist ovecomes the disadvantage caused by arrays..
the main disadvantage is arrays cannot be resized..it is static .once created then it should be used as it is..we cannot increase or decrease size where as Arraylist comes with dynamic creation so we can have elements as much we like there is no need to specify the size at starting..and more over we can store usedefined objects in Arraylist...like for example if
we create a Arraylist of type car..
syntax:
List<Car>obj=new ArrayList<Car>();
obj.add(carobj);
we can store of object of respective car class in Arraylist ..
and in arrays we can store elements of only primitive data types like int,char,double ,long
string ..
in arraylist we can store primitive types as well as objects..
3)suppose a programming is writing a program in java..
example he had an array of 10 elements of integer type ...and array contains elements less than 100..now programer want to store the elements which are less than 50 in a new array which is a type integer..here the problem arises ..becase we dont know the count of elements how many are less than 50..so now we cannot use arrays here...
for this type of problems we can use arraylist because the count may be unknown ...the arraylist can store elements ..
4)Arraylist class is implements List interface ..Arraylist comes with java collection framework..
Array list is developed due to some disadvantages caused by Arrays ..like
Arrays cannot be added new elments once after creation..
and cannot be srink later...
to avoid these disadvantages Arraylist comes with more flexibility...
it can add new elements when ever we want ..
and even we can srink size of Arraylist..
Arraylist stores objects ....and also stores user defined objects...
inorder to make job easy Arraylist comes with best methods..with that..
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.