0% found this document useful (0 votes)
6 views3 pages

DAY6 Notes

The document provides an overview of arrays and collections in Java, detailing how to create and manipulate arrays, their advantages and disadvantages, and methods for comparing and sorting them. It also discusses the differences between ArrayList and Vector, as well as between ArrayList and LinkedList, and includes information on converting between arrays and lists. Key concepts such as array initialization, default values, and the use of generics are also covered.

Uploaded by

tikitakam40
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

DAY6 Notes

The document provides an overview of arrays and collections in Java, detailing how to create and manipulate arrays, their advantages and disadvantages, and methods for comparing and sorting them. It also discusses the differences between ArrayList and Vector, as well as between ArrayList and LinkedList, and includes information on converting between arrays and lists. Key concepts such as array initialization, default values, and the use of generics are also covered.

Uploaded by

tikitakam40
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

DAY6:

1. Arrays , 2. Collections Introduction, 3. List(ArrayList only)

QUESTIONS(Theory)

1. What do you mean by an array and How to create an Array?

Arrays are used to store multiple values in a single variable, instead of declaring separate variables
for each value. To create an array, define the data type (like int ) and specify the name of the array
followed by square brackets [].

2. Can you change size of array once created?

You can't change the size of an array in Java once the array is initialized. Java arrays do not expand
and contract.

3. Is it legal to initialize an array int i[] = {1, 2, 3, 4, 5} ?

Yes, it is perfectly legal. You can create and initialize array in same line in Java.

4. Advantages and disadvantages of Array?

Advantages of Array Data Structure:

Efficient and Fast Access: Arrays allow direct and efficient access to any element in the collection
with constant access time, as the data is stored in contiguous memory locations.

Memory Efficiency: Arrays store elements in contiguous memory, allowing efficient allocation in a
single block and reducing memory fragmentation.

Versatility: Arrays can be used to store a wide range of data types, including integers, floating-point
numbers, characters, and even complex data structures such as objects and pointers.

Compatibility with hardware: The array data structure is compatible with most hardware
architectures, making it a versatile tool for programming in a wide range of environments.

Disadvantages of Array Data Structure:

Fixed Size: Arrays have a fixed size set at creation. Expanding an array requires creating a new one
and copying elements, which is time-consuming and memory-intensive.

Memory Allocation Issues: Allocating large arrays can cause memory exhaustion, leading to crashes,
especially on systems with limited resources.

Insertion and Deletion Challenges: Adding or removing elements requires shifting subsequent
elements, making these operations inefficient.

Limited Data Type Support: Arrays support only elements of the same type, limiting their use with
complex data types.

Lack of Flexibility: Fixed size and limited type support make arrays less adaptable than structures like
linked lists or trees.

5. Can we change the size of an array at run time?

The size of the array is determined at the time of its creation or, initialization. Once it is done you
cannot change the size of the array.
6. Can you declare an array without assigning the size of an array?

Yes. We can declare an array without size but before using it needs to be initialized.

7. Can you declare a negative array size of an array?

Array dimensions cannot have a negative size.

8. What is the default value of Array?

Array elements are initialized to 0 if they are a numeric type ( int or double ), false if they are of type
boolean , or null if they are an object type like String .

9. How to print element of Array?

There are two methods to Print an Array in Java as mentioned below:

 Using for loop


 Using standard library arrays
 Using while loop
 Using forEach() method

11. How to compare Two Arrays?

Some Ways to Compare Two Arrays in Java

 Using Arrays.equals() Method.


 Using Arrays.deepEquals() for Nested Arrays.
 Using Arrays.deepEquals() for Two Multidimensional Arrays.

12. How to sort an Array?

Arrays sort() method is used for sorting the elements in an Array. It consists of two variations, one in
which it sorts the entire array, it may be an integer or character array.

Another one allows sorting a specific part of the array by passing the starting and ending indices.

 To sort an array in descending order, we can


use Arrays.sort() method with Collections.reverseOrder() as a comparator.
 To sort an array of strings in descending alphabetical order, the Arrays.sort() method
combined with Collections.reverseOrder() method and it arranges the strings from Z to A
based on lexicographical order.

13. Can we declare array size as a negative number?

No, you cannot declare an array size as a negative number in Java; attempting to do so will result in a
runtime error called "NegativeArraySizeException" because an array size must always be a positive
integer value.

14. Can we add or delete an element after assigning an array?

Arrays in Java are immutable. To add or remove elements, you have to create a new array. We can
assign it to the variable referencing the old array, but you cannot do this to a method argument. We
may want to change from arrays to List .
15. Can we use Generics with the array?

Yes, we can create a generic array that functions with various object types by utilizing generics. We
can build type-safe, reusable code by using generics.

16. What is collection and explain about types?

Java Collections framework provides implementation classes for core collection interfaces. We can
use them to create different types of collections in the Java program. Some important collection
classes are ArrayList, LinkedList, HashMap, TreeMap, HashSet, and TreeSet.

17. What is the difference between ArrayList and Vector?

ArrayList and Vector are both classes that implement dynamic arrays, but they differ in how they
grow and in whether they are synchronized.

 Synchronization

A synchronized class ensures that only one thread can access the object at a time.

 Growth

When an ArrayList is overextended, it increases the size of the array by 50%. When a
Vector is overextended, it increases the size of the array by doubling.

 Performance

ArrayList is generally faster than Vector because it is not synchronized.

 Suitability
ArrayList is generally preferred for single-threaded environments, whereas Vector is
suitable for multi-threaded scenarios.
Both ArrayList and Vector implement the List interface
18. What is the difference between ArrayList and LinkedList?

 An array is a collection of elements stored in a contiguous block of memory, allowing fast


access to any element.
 A linked list, on the other hand, stores elements in nodes that are connected by pointers,
allowing for flexible memory usage.

19. How to convert Array to List and List to Array?

Arrays. asList(array)

This method will take in a standard array and wrap it in the AbstractList class, exposing all the
methods available to objects that implement the List interface.

Methods to Convert List to Array in Java

Using get() method, Using toArray() method

You might also like