Methods, Arrays, and Program Structure
The Arrays Utility Class and Common Array Operations
You are viewing a free preview lesson.
Lesson Overview
আগের lessons-এ আমরা arrays manually traverse, search, copy, compare, reverse, এবং process করেছি।
Example:
int[] numbers = {
10,
20,
30
};
অনেক common array operation আমরা নিজেরাই loop লিখে করতে পারি।
কিন্তু Java standard library already provides a useful utility class:
java.util.Arrays
এতে arrays-এর জন্য অনেক common operations built in আছে।
Examples:
Arrays.toString(...)
Arrays.equals(...)
Arrays.copyOf(...)
Arrays.fill(...)
Arrays.sort(...)
Arrays.binarySearch(...)
এই lesson-এ আমরা শিখব:
- What
java.util.Arraysis - Importing utility classes
- Printing arrays
- Comparing arrays
- Copying arrays
- Copying ranges
- Filling arrays
- Sorting arrays
- Searching sorted arrays
- Working with 2D arrays
deepToString()deepEquals()- Object arrays
- Primitive array differences
- When built-in utilities are better than manual loops
- How these APIs connect to algorithms
What Is java.util.Arrays?
Arrays is a utility class from the Java standard library।
It contains static methods for working with arrays।
Example:
Arrays.sort(
numbers
);
We do not need to create:
new Arrays()
Instead, we call methods using the class name:
Arrays.sort(...)
This connects directly to what we learned about static methods।
Importing Arrays
At the top of the file:
import java.util.Arrays;
Then:
Arrays.toString(
numbers
);
can be used।
Complete Example
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] numbers = {
30,
10,
20
};
Arrays.sort(
numbers
);
System.out.println(
Arrays.toString(
numbers
)
);
}
}
Output:
[10, 20, 30]
Why Use Standard Library Utilities?
Suppose we want to print an array।
We could write:
for (
int number
: numbers
) {
System.out.println(
number
);
}
But if we simply need a readable representation:
Arrays.toString(
numbers
);
is shorter and clearer।
The principle is:
Understand the underlying operation
but use proven standard APIs
when they express the intent clearly.
Arrays.toString()
Directly printing an array:
int[] numbers = {
1,
2,
3
};
System.out.println(
numbers
);
does not print the contents nicely।
You may see something similar to:
[I@5acf9800
Proper Array Printing
System.out.println(
Arrays.toString(
numbers
)
);
Output:
[1, 2, 3]
Arrays.toString() with Strings
String[] courses = {
"Java",
"Backend",
"System Design"
};
System.out.println(
Arrays.toString(
courses
)
);
Output:
[Java, Backend, System Design]
Arrays.toString() Is Mainly for One-Dimensional Arrays
Consider:
int[][] matrix = {
{1, 2},
{3, 4}
};
This:
Arrays.toString(
matrix
);
does not recursively print inner array contents in the most useful form।
For nested arrays we use:
Arrays.deepToString(...)
Arrays.deepToString()
Example:
int[][] matrix = {
{1, 2},
{3, 4}
};
System.out.println(
Arrays.deepToString(
matrix
)
);
Output:
[[1, 2], [3, 4]]
When to Use deepToString()
Use it for nested reference array structures such as:
int[][]
String[][]
Object[][]
or deeper dimensions:
int[][][]
Example:
System.out.println(
Arrays.deepToString(
data
)
);
Arrays.equals()
Suppose:
int[] first = {
1,
2,
3
};
int[] second = {
1,
2,
3
};
This:
first == second
returns:
false
because these are different array objects।
Compare Array Contents
Use:
Arrays.equals(
first,
second
);
Result:
true
because corresponding elements are equal।
Example
int[] first = {
1,
2,
3
};
int[] second = {
1,
2,
4
};
boolean same =
Arrays.equals(
first,
second
);
System.out.println(
same
);
Output:
false
Equality Includes Order
These arrays:
int[] first = {
1,
2,
3
};
int[] second = {
3,
2,
1
};
contain the same values but in different order।
Arrays.equals(
first,
second
);
returns:
false
Array equality is positional।
Length Must Also Match
int[] first = {
1,
2
};
int[] second = {
1,
2,
0
};
These are not equal।
Arrays.deepEquals()
For nested arrays:
int[][] first = {
{1, 2},
{3, 4}
};
int[][] second = {
{1, 2},
{3, 4}
};
Use:
Arrays.deepEquals(
first,
second
);
Result:
true
Why Not Just Arrays.equals() for 2D Arrays?
Remember:
int[][]
is an array of int[] references।
A shallow comparison of outer arrays compares those row elements as objects/references according to the applicable equality behavior।
For recursive nested-content comparison:
Arrays.deepEquals(...)
is the appropriate API।
Arrays.copyOf()
Suppose:
int[] original = {
10,
20,
30
};
We want an independent copy।
Use:
int[] copy =
Arrays.copyOf(
original,
original.length
);
Verify Independence
copy[0] =
999;
System.out.println(
original[0]
);
Output:
10
because the primitive array elements were copied into a new array।
copyOf() Takes a New Length
Signature conceptually:
Arrays.copyOf(
original,
newLength
)
This means the copied array does not have to have the same length।
Copy into a Larger Array
int[] original = {
10,
20,
30
};
int[] copy =
Arrays.copyOf(
original,
5
);
Result:
[10, 20, 30, 0, 0]
New positions receive the normal default value।
Copy into a Smaller Array
int[] original = {
10,
20,
30,
40
};
int[] copy =
Arrays.copyOf(
original,
2
);
Result:
[10, 20]
Extra elements are discarded from the copy।
Why This Is Useful
Arrays are fixed-size।
You cannot resize an existing array।
But you can create another array with a different size:
numbers =
Arrays.copyOf(
numbers,
numbers.length + 1
);
This does not resize the old array।
It creates a new one and reassigns the reference।
Manual Dynamic Growth Is Expensive
If you repeatedly do:
Arrays.copyOf(
array,
array.length + 1
);
for every insertion, many array copies may occur।
This is one reason classes like:
ArrayList
exist।
They manage dynamic array growth for us।
Arrays.copyOfRange()
Sometimes we need only part of an array।
Example:
int[] numbers = {
10,
20,
30,
40,
50
};
Copy:
int[] middle =
Arrays.copyOfRange(
numbers,
1,
4
);
Result:
[20, 30, 40]
Range Convention
The range is:
from inclusive
to exclusive
So:
copyOfRange(
numbers,
1,
4
)
includes:
1
2
3
but not:
4
This Convention Appears Everywhere
Java frequently uses:
[start, end)
meaning:
start included
end excluded
You will see this idea in:
substring
subList
streams
ranges
copy operations
It is worth becoming comfortable with it।
Arrays.fill()
Suppose we want every element to contain the same value।
Instead of:
for (
int i = 0;
i < numbers.length;
i++
) {
numbers[i] =
-1;
}
we can use:
Arrays.fill(
numbers,
-1
);
Example
int[] numbers =
new int[5];
Arrays.fill(
numbers,
7
);
System.out.println(
Arrays.toString(
numbers
)
);
Output:
[7, 7, 7, 7, 7]
Filling Reference Arrays
String[] statuses =
new String[3];
Arrays.fill(
statuses,
"PENDING"
);
Result:
[PENDING, PENDING, PENDING]
Be Careful with Mutable Objects
Suppose later:
SomeMutableObject object =
new SomeMutableObject();
Arrays.fill(
objects,
object
);
Every position receives the same object reference।
It does not create a separate object for each element।
This is similar to reference behavior we already learned।
Filling a Range
Arrays.fill() also has overloads that operate on a range।
Conceptually:
Arrays.fill(
numbers,
fromIndex,
toIndex,
value
);
Example:
int[] numbers = {
1,
2,
3,
4,
5
};
Arrays.fill(
numbers,
1,
4,
0
);
Result:
[1, 0, 0, 0, 5]
Again:
1 included
4 excluded
Arrays.sort()
One of the most common utilities is sorting।
Example:
int[] numbers = {
40,
10,
30,
20
};
Arrays.sort(
numbers
);
After sorting:
[10, 20, 30, 40]
Arrays.sort() Mutates the Array
This is important।
Before:
[40, 10, 30, 20]
After:
[10, 20, 30, 40]
The original array itself changes।
Preserve Original Before Sorting
If you don't want to modify the original:
int[] sorted =
Arrays.copyOf(
numbers,
numbers.length
);
Arrays.sort(
sorted
);
Now:
numbers → original order
sorted → sorted order
Sorting Strings
String[] names = {
"Sumu",
"Sakib",
"Jalisa",
"Subu"
};
Arrays.sort(
names
);
Java sorts using the type's natural ordering।
For strings this is lexicographic ordering based on their comparison rules।
Natural Ordering
Many Java types define a natural order।
Examples:
Integer → numeric order
Long → numeric order
String → lexicographic order
Later, for custom objects, we will learn:
Comparable
Comparator
for defining ordering behavior।
Sorting Part of an Array
Arrays.sort() has overloads that accept ranges।
Example:
int[] numbers = {
9,
4,
3,
2,
8
};
Arrays.sort(
numbers,
1,
4
);
Only positions:
1
2
3
are sorted।
Original segment:
4 3 2
becomes:
2 3 4
Final array:
[9, 2, 3, 4, 8]
Sorting Algorithms
We will later implement and understand algorithms such as:
Bubble Sort
Insertion Sort
Selection Sort
Merge Sort
Quick Sort
So why use:
Arrays.sort(...)
?
Because production code should usually use proven standard-library implementations unless there is a specific reason not to।
Learning manual sorting teaches:
How algorithms work
Why complexity matters
How comparisons and swaps operate
Using Arrays.sort() teaches:
How professional Java code gets common work done.
Both are important।
Arrays.binarySearch()
Java also provides binary search।
Example:
int[] numbers = {
10,
20,
30,
40,
50
};
int index =
Arrays.binarySearch(
numbers,
30
);
System.out.println(
index
);
Output:
2
Critical Requirement: The Array Must Be Sorted
Binary search assumes ordered data।
Correct:
int[] numbers = {
10,
20,
30,
40
};
Arrays.binarySearch(
numbers,
30
);
Wrong Usage
int[] numbers = {
30,
10,
40,
20
};
Arrays.binarySearch(
numbers,
20
);
The result is not meaningful according to the method contract because the array is not sorted as required।
Do not call binary search on unsorted data and expect reliable search semantics।
Safe Pattern
int[] numbers = {
30,
10,
40,
20
};
Arrays.sort(
numbers
);
int index =
Arrays.binarySearch(
numbers,
20
);
Binary Search Result When Found
If found:
return value >= 0
and it represents an index।
Example:
int index =
Arrays.binarySearch(
numbers,
30
);
might return:
2
What If the Value Is Missing?
Arrays.binarySearch() does not simply return -1 for every missing value।
It returns a negative value encoding the position where the value could be inserted while preserving sorted order।
You do not need to memorize the exact formula yet।
For basic existence checking:
int index =
Arrays.binarySearch(
numbers,
target
);
boolean found =
index >= 0;
is enough।
Example
int[] numbers = {
10,
20,
30
};
int index =
Arrays.binarySearch(
numbers,
99
);
if (
index >= 0
) {
System.out.println(
"Found"
);
} else {
System.out.println(
"Not found"
);
}
Why Binary Search Is Interesting
Our manual search checked elements one by one:
Linear search
Binary search repeatedly narrows the search space।
For large sorted datasets, it can be significantly more efficient।
We'll study exactly why in the Algorithms module।
Arrays.compare()
Modern Java also provides array comparison APIs such as:
Arrays.compare(...)
This compares arrays lexicographically।
Example:
int[] first = {
1,
2,
3
};
int[] second = {
1,
2,
4
};
int result =
Arrays.compare(
first,
second
);
The result will be:
negative → first comes before second
zero → equal ordering
positive → first comes after second
Equality vs Ordering Comparison
Do not confuse:
Arrays.equals(...)
with:
Arrays.compare(...)
equals() asks:
Are contents equal?
compare() asks:
How do these arrays order relative to each other?
Arrays.mismatch()
Another useful API:
Arrays.mismatch(
first,
second
);
It returns the first index where the arrays differ।
Example:
int[] first = {
10,
20,
30
};
int[] second = {
10,
99,
30
};
int index =
Arrays.mismatch(
first,
second
);
Result:
1
If There Is No Mismatch
For equal arrays:
Arrays.mismatch(
first,
second
);
returns:
-1
This is useful when debugging or comparing structured data।
Arrays of Objects
Arrays methods also work with reference arrays।
Example:
String[] names = {
"Sakib",
"Subu",
"Sumu"
};
String[] copy =
Arrays.copyOf(
names,
names.length
);
But remember:
The array structure is copied.
The referenced objects are not automatically deep-copied.
Reference Array Copy Is Shallow
Suppose:
Course[] courses =
...
Then:
Course[] copy =
Arrays.copyOf(
courses,
courses.length
);
creates a new Course[] array।
But corresponding positions still point to the same Course objects।
Conceptually:
original[0] ----\
→ Course A
copy[0] --------/
This is called a:
Shallow copy
Primitive Arrays Are Different
For:
int[]
elements are primitive values।
So copying the array copies those primitive values directly।
There are no nested objects to share at the element level।
Nested Arrays Still Need Deep-Copy Thinking
For:
int[][]
the outer array contains references to inner int[] arrays।
So:
Arrays.copyOf(
matrix,
matrix.length
);
copies only the outer array structure।
Rows remain shared।
This is why we manually copied each row in the previous lesson।
Arrays.deepToString() Is Not Deep Copy
Do not confuse:
deepToString()
deepEquals()
with:
deep copying
They recursively inspect nested contents for printing/comparison।
They do not create copied nested structures।
Arrays.asList() — Important Caveat
You may encounter:
Arrays.asList(
"Java",
"Backend",
"System Design"
);
This creates a list backed by an array-like fixed-size structure।
But this deserves careful treatment once we study Collections।
For now remember:
Arrays.asList() does not behave like a normal resizable ArrayList.
Also, primitive arrays have another important behavior with Arrays.asList() that can surprise beginners।
We will cover it properly in the Collections module rather than introducing partial knowledge here।
Arrays.stream() — Preview
Modern Java provides:
Arrays.stream(
numbers
);
This creates a stream for array processing।
Example:
int sum =
Arrays.stream(
numbers
)
.sum();
But Stream API has its own programming model।
We will study streams properly in the Modern Java module।
For now:
Do not replace learning loops with streams before understanding traversal.
Built-In Utility vs Manual Implementation
Suppose we need a sorted array।
Option 1:
// manually implement sorting algorithm
Option 2:
Arrays.sort(
numbers
);
Which should production code use?
Usually:
Arrays.sort(...)
unless you have a specific requirement।
But Why Learn Algorithms Then?
Because using an API without understanding its behavior can cause mistakes।
For example:
Arrays.binarySearch(...)
requires sorted input।
If you don't understand binary search, that requirement may feel arbitrary।
Algorithm knowledge helps you understand:
Why APIs have certain contracts
What performance characteristics to expect
Which data structure is appropriate
Example: Copy, Sort, Search
A common safe workflow:
static boolean containsSorted(
int[] values,
int target
) {
int[] sorted =
Arrays.copyOf(
values,
values.length
);
Arrays.sort(
sorted
);
return Arrays.binarySearch(
sorted,
target
) >= 0;
}
This preserves the original array।
But Is That Always Efficient?
No।
If you search only once, sorting first may cost more work than a simple linear search।
Example:
One unsorted array
One target lookup
Manual linear search may be simpler and cheaper।
But if you have:
One array
Thousands of repeated searches
sorting once and using binary search may make more sense।
This is exactly the kind of tradeoff we will study in the Algorithms module।
Arrays.sort() and Mutation
Consider:
int[] original = {
3,
1,
2
};
Arrays.sort(
original
);
If another part of the program expects original ordering, you have changed shared state।
When mutation matters, be explicit:
int[] sorted =
Arrays.copyOf(
original,
original.length
);
Arrays.sort(
sorted
);
Utility APIs Do Not Replace Design Decisions
An API can tell you:
How to sort
but not:
Should this data be sorted?
Should original ordering be preserved?
Should duplicate values remain?
Should null values be allowed?
These remain application requirements।
Working with char[]
Arrays utilities work with many primitive array types।
Example:
char[] letters = {
'c',
'a',
'b'
};
Arrays.sort(
letters
);
System.out.println(
Arrays.toString(
letters
)
);
Output:
[a, b, c]
Working with double[]
double[] prices = {
19.99,
5.50,
12.75
};
Arrays.sort(
prices
);
Result:
[5.5, 12.75, 19.99]
Useful Methods Summary
Some of the most useful Arrays APIs:
Arrays.toString()
Arrays.deepToString()
Arrays.equals()
Arrays.deepEquals()
Arrays.compare()
Arrays.mismatch()
Arrays.copyOf()
Arrays.copyOfRange()
Arrays.fill()
Arrays.sort()
Arrays.binarySearch()
Later:
Arrays.stream()
Arrays.asList()
will connect to Streams and Collections।
Practical Example: Score Processing
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] scores = {
82,
91,
76,
88,
69
};
System.out.println(
"Original: "
+ Arrays.toString(
scores
)
);
int[] sortedScores =
Arrays.copyOf(
scores,
scores.length
);
Arrays.sort(
sortedScores
);
System.out.println(
"Sorted: "
+ Arrays.toString(
sortedScores
)
);
int index =
Arrays.binarySearch(
sortedScores,
88
);
System.out.println(
"88 found at index: "
+ index
);
System.out.println(
"Original still: "
+ Arrays.toString(
scores
)
);
}
}
Possible output:
Original: [82, 91, 76, 88, 69]
Sorted: [69, 76, 82, 88, 91]
88 found at index: 3
Original still: [82, 91, 76, 88, 69]
Practical Example: Comparing Course Codes
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
String[] first = {
"JAVA",
"BACKEND"
};
String[] second = {
"JAVA",
"BACKEND"
};
System.out.println(
first == second
);
System.out.println(
Arrays.equals(
first,
second
)
);
}
}
Output:
false
true
This reinforces:
Reference identity
vs
Content equality
Practical Example: Matrix Output
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[][] matrix = {
{1, 2, 3},
{4, 5, 6}
};
System.out.println(
Arrays.deepToString(
matrix
)
);
}
}
Output:
[[1, 2, 3], [4, 5, 6]]
Common Beginner Mistake 1: Forgetting the Import
If you use:
Arrays.sort(...)
without:
import java.util.Arrays;
and without the fully qualified name, Java cannot resolve Arrays।
Alternative:
java.util.Arrays.sort(
numbers
);
but importing is cleaner for repeated use।
Common Beginner Mistake 2: Assuming toString() Is Deep
For:
int[][]
use:
Arrays.deepToString(...)
not only:
Arrays.toString(...)
if you want recursive content output।
Common Beginner Mistake 3: Using == for Array Content
Wrong:
first == second
if your question is:
Do these arrays contain equal values?
Use:
Arrays.equals(...)
Common Beginner Mistake 4: Using equals() Directly on Primitive Arrays
This:
first.equals(
second
);
does not perform element-by-element primitive array comparison the way many beginners expect।
Use:
Arrays.equals(
first,
second
);
Common Beginner Mistake 5: Sorting When Original Order Matters
Arrays.sort(
values
);
mutates the supplied array।
Copy first if order must be preserved।
Common Beginner Mistake 6: Binary Search on Unsorted Data
This violates the API contract:
Arrays.binarySearch(
unsortedValues,
target
);
Sort first or use linear search।
Common Beginner Mistake 7: Thinking copyOf() Is Always Deep
For:
Object[]
Nested arrays
copied elements may still reference the same objects।
Common Beginner Mistake 8: Misunderstanding Exclusive End Index
For:
Arrays.copyOfRange(
numbers,
1,
4
);
index 4 is not included।
Think:
[1, 4)
Common Beginner Mistake 9: Assuming Binary Search Always Returns -1
When not found, the result is negative, but not necessarily exactly:
-1
If you only need existence:
result >= 0
is the correct basic check।
Common Beginner Mistake 10: Reimplementing Everything
If production code needs:
Copy array
Sort array
Compare contents
Fill values
writing custom loops every time creates unnecessary code and more opportunities for bugs।
Use standard library APIs when they clearly solve the requirement।
Practice 1: Print an Array
Given:
int[] numbers = {
5,
10,
15
};
print:
[5, 10, 15]
Solution
System.out.println(
Arrays.toString(
numbers
)
);
Practice 2: Compare Contents
Given:
int[] first = {
1,
2,
3
};
int[] second = {
1,
2,
3
};
check whether contents match।
Solution
boolean equal =
Arrays.equals(
first,
second
);
Practice 3: Copy an Array
Create an independent copy of:
String[] courses = {
"Java",
"Backend"
};
Solution
String[] copy =
Arrays.copyOf(
courses,
courses.length
);
The array objects are separate, although referenced String objects may be shared safely because String is immutable।
Practice 4: Copy a Range
Given:
int[] numbers = {
10,
20,
30,
40,
50
};
create:
[20, 30, 40]
Solution
int[] result =
Arrays.copyOfRange(
numbers,
1,
4
);
Practice 5: Fill an Array
Create a five-element array where every value is:
-1
Solution
int[] values =
new int[5];
Arrays.fill(
values,
-1
);
Practice 6: Sort Without Modifying Original
Given:
int[] numbers = {
3,
1,
2
};
produce a sorted copy while keeping numbers unchanged।
Solution
int[] sorted =
Arrays.copyOf(
numbers,
numbers.length
);
Arrays.sort(
sorted
);
Practice 7: Binary Search
Given:
int[] numbers = {
10,
20,
30,
40
};
check whether 30 exists using Arrays.binarySearch()।
Solution
boolean found =
Arrays.binarySearch(
numbers,
30
) >= 0;
Practice 8: Nested Array Printing
Given:
int[][] values = {
{1, 2},
{3, 4}
};
print nested contents।
Solution
System.out.println(
Arrays.deepToString(
values
)
);
Practice 9: Nested Equality
Check whether:
int[][] first = {
{1, 2},
{3, 4}
};
int[][] second = {
{1, 2},
{3, 4}
};
contain equal nested values।
Solution
boolean equal =
Arrays.deepEquals(
first,
second
);
Practice 10: Predict the Output
int[] numbers = {
3,
1,
2
};
int[] copy =
Arrays.copyOf(
numbers,
numbers.length
);
Arrays.sort(
copy
);
System.out.println(
Arrays.toString(
numbers
)
);
System.out.println(
Arrays.toString(
copy
)
);
Answer
[3, 1, 2]
[1, 2, 3]
Practice 11: Predict the Result
int[] first = {
1,
2
};
int[] second = {
1,
2
};
System.out.println(
first == second
);
System.out.println(
Arrays.equals(
first,
second
)
);
Answer
false
true
Practice 12: Find the Bug
int[] values = {
50,
10,
30
};
int index =
Arrays.binarySearch(
values,
30
);
What's wrong?
Answer
The array is not sorted।
Arrays.binarySearch() requires data sorted according to the compatible ordering।
Correct:
Arrays.sort(
values
);
int index =
Arrays.binarySearch(
values,
30
);
Or use a linear search if preserving original order or avoiding sorting is more appropriate।
True or False
Arraysis part of the Java standard library.- Most common
Arraysutilities are static methods. Arrays.toString()is useful for one-dimensional arrays.Arrays.deepToString()is useful for nested arrays.==compares array contents element by element.Arrays.equals()considers element order.Arrays.copyOf()can create a different-length array.Arrays.sort()always returns a new array.Arrays.binarySearch()requires sorted input.- A negative
binarySearch()result always means exactly-1. - Copying an object array automatically deep-copies every object.
- Standard library utilities are generally preferable when they clearly solve a common operation.
Answers
1. True
2. True
3. True
4. True
5. False
6. True
7. True
8. False
9. True
10. False
11. False
12. True
Knowledge Check
Question 1
What is java.util.Arrays?
Question 2
Why does directly printing an array not normally display its contents clearly?
Question 3
What is the difference between Arrays.toString() and Arrays.deepToString()?
Question 4
What is the difference between == and Arrays.equals() for arrays?
Question 5
What does Arrays.copyOf() do?
Question 6
What does [from, to) mean in range-based APIs?
Question 7
Does Arrays.sort() mutate its argument?
Question 8
What should you do if original ordering must be preserved before sorting?
Question 9
What requirement does Arrays.binarySearch() have?
Question 10
How can you check whether binarySearch() found a value?
Question 11
Why is copying an object array usually a shallow copy?
Question 12
Why should developers learn manual algorithms if Java already provides sorting and searching APIs?
Knowledge Check Answers
Answer 1
java.util.Arrays is a standard-library utility class providing static operations for working with arrays।
Answer 2
Arrays inherit object-style string representation rather than automatically formatting every element as human-readable content।
Answer 3
toString() formats a one-dimensional array, while deepToString() recursively formats nested arrays।
Answer 4
== checks whether two variables reference the same array object, while Arrays.equals() compares corresponding array contents।
Answer 5
It creates a new array and copies elements from the source, optionally using a different requested length।
Answer 6
The starting index is included and the ending index is excluded।
Answer 7
Yes. It rearranges the elements of the provided array।
Answer 8
Create a copy first and sort the copy।
Answer 9
The input must already be sorted according to the compatible ordering expected by the search।
Answer 10
For basic existence checking:
Arrays.binarySearch(
values,
target
) >= 0
means the target was found।
Answer 11
The new array receives copies of the object references, not independently cloned objects।
Answer 12
Algorithm knowledge explains performance, contracts, tradeoffs, and why APIs behave the way they do, while standard-library APIs provide reliable production implementations।
Lesson Summary
এই lesson-এ আমরা manual array processing-এর পাশাপাশি Java standard library-এর built-in array utilities শিখেছি।
We learned:
java.util.Arraysis a static utility classArrays.toString()formats one-dimensional arraysArrays.deepToString()handles nested array structuresArrays.equals()compares array contentsArrays.deepEquals()recursively compares nested arraysArrays.compare()supports ordering comparisonsArrays.mismatch()finds the first differing indexArrays.copyOf()creates a new array- Copies can be shorter or larger than the source
Arrays.copyOfRange()copies a selected range- Range APIs commonly use inclusive-start/exclusive-end conventions
Arrays.fill()assigns a common value across elementsArrays.sort()sorts the original array in place- Copy before sorting when original order must remain unchanged
Arrays.binarySearch()searches sorted arrays efficiently- A negative binary-search result means the target was not found
- Object-array copies are shallow with respect to referenced objects
- Nested arrays require deeper copying when structural independence matters
- Standard utility APIs should usually be preferred over rewriting common production operations
- Understanding manual algorithms is still necessary for reasoning about complexity and API contracts
The important engineering idea is:
Learn how an operation works.
Then use the standard library
when it already provides a clear,
tested implementation.
Next Lesson
পরবর্তী lesson:
Introduction to Recursion
আমরা শিখব:
- What recursion means
- Recursive method calls
- Base case
- Recursive case
- Call stack intuition
- Tracing recursive execution
- Factorial
- Sum of numbers
- Working with arrays recursively
- Infinite recursion
- Stack overflow
- Recursion vs loops
- When recursion is useful and when iteration is simpler