Skip to content

Commit 06965dd

Browse files
authored
Merge pull request #140 from shreyyq/java-tutorials
Java tutorials
2 parents 264a9e8 + 84eec58 commit 06965dd

34 files changed

+4153
-39
lines changed

docs/java/arrays-and-collections/array-lists-and-collections-framework.md

Lines changed: 147 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,150 @@ sidebar_label: ArrayLists and Collections Framework
55
sidebar_position: 2
66
tags: [java, array-lists-and-collections-framework]
77
description: In this tutorial, you will learn about ArrayLists and the Collections Framework in Java.
8-
---
8+
---
9+
10+
# ArrayLists and the Collections Framework in Java
11+
12+
## Introduction
13+
14+
Java's Collections Framework provides a set of classes and interfaces for storing and manipulating groups of data as a single unit. `ArrayList` is one of the most commonly used classes in this framework, offering dynamic arrays that can grow and shrink in size.
15+
16+
## ArrayList
17+
18+
An `ArrayList` is a resizable array that provides more functionality and flexibility compared to a standard array. It is part of the `java.util` package.
19+
20+
### Declaration
21+
22+
To use `ArrayList`, you need to import it from the `java.util` package.
23+
24+
### Syntax
25+
26+
```java
27+
import java.util.ArrayList;
28+
29+
ArrayList<dataType> arrayListName = new ArrayList<>();
30+
```
31+
32+
### Example
33+
34+
```java
35+
import java.util.ArrayList;
36+
37+
public class Main {
38+
public static void main(String[] args) {
39+
ArrayList<String> names = new ArrayList<>();
40+
41+
// Adding elements
42+
names.add("Alice");
43+
names.add("Bob");
44+
names.add("Charlie");
45+
46+
// Accessing elements
47+
System.out.println(names.get(0)); // Outputs: Alice
48+
49+
// Modifying elements
50+
names.set(1, "Robert");
51+
52+
// Removing elements
53+
names.remove(2);
54+
55+
// Iterating over the ArrayList
56+
for (String name : names) {
57+
System.out.println(name);
58+
}
59+
}
60+
}
61+
```
62+
63+
### Common Methods
64+
65+
- `add(element)`: Adds an element to the end of the list.
66+
- `add(index, element)`: Inserts an element at the specified index.
67+
- `get(index)`: Returns the element at the specified index.
68+
- `set(index, element)`: Replaces the element at the specified index with the specified element.
69+
- `remove(index)`: Removes the element at the specified index.
70+
- `remove(element)`: Removes the first occurrence of the specified element.
71+
- `size()`: Returns the number of elements in the list.
72+
- `clear()`: Removes all elements from the list.
73+
- `isEmpty()`: Returns `true` if the list contains no elements.
74+
75+
## Collections Framework
76+
77+
The Collections Framework provides a unified architecture for representing and manipulating collections. It includes interfaces, implementations, and algorithms.
78+
79+
### Interfaces
80+
81+
- `Collection`: The root interface for all collections.
82+
- `List`: An ordered collection (also known as a sequence). `ArrayList` and `LinkedList` are its implementations.
83+
- `Set`: A collection that contains no duplicate elements. `HashSet` and `TreeSet` are its implementations.
84+
- `Queue`: A collection used to hold multiple elements prior to processing. `LinkedList` and `PriorityQueue` are its implementations.
85+
- `Map`: An object that maps keys to values. `HashMap` and `TreeMap` are its implementations.
86+
87+
### Example: Using Different Collections
88+
89+
```java
90+
import java.util.*;
91+
92+
public class Main {
93+
public static void main(String[] args) {
94+
// List example
95+
List<String> list = new ArrayList<>();
96+
list.add("Apple");
97+
list.add("Banana");
98+
list.add("Orange");
99+
System.out.println("List: " + list);
100+
101+
// Set example
102+
Set<String> set = new HashSet<>();
103+
set.add("Apple");
104+
set.add("Banana");
105+
set.add("Apple"); // Duplicate element will not be added
106+
System.out.println("Set: " + set);
107+
108+
// Queue example
109+
Queue<String> queue = new LinkedList<>();
110+
queue.add("Apple");
111+
queue.add("Banana");
112+
queue.add("Orange");
113+
System.out.println("Queue: " + queue);
114+
System.out.println("Queue poll: " + queue.poll()); // Removes and returns the head of the queue
115+
116+
// Map example
117+
Map<String, Integer> map = new HashMap<>();
118+
map.put("Apple", 1);
119+
map.put("Banana", 2);
120+
map.put("Orange", 3);
121+
System.out.println("Map: " + map);
122+
System.out.println("Map get: " + map.get("Banana")); // Returns the value for the specified key
123+
}
124+
}
125+
```
126+
127+
### Algorithms
128+
129+
The `Collections` class provides static methods that operate on collections, such as sorting and searching.
130+
131+
#### Example: Sorting a List
132+
133+
```java
134+
import java.util.*;
135+
136+
public class Main {
137+
public static void main(String[] args) {
138+
List<String> list = new ArrayList<>();
139+
list.add("Banana");
140+
list.add("Apple");
141+
list.add("Orange");
142+
143+
Collections.sort(list); // Sorts the list in natural order
144+
System.out.println("Sorted List: " + list);
145+
146+
Collections.sort(list, Collections.reverseOrder()); // Sorts the list in reverse order
147+
System.out.println("Reverse Sorted List: " + list);
148+
}
149+
}
150+
```
151+
152+
## Conclusion
153+
154+
The Collections Framework in Java provides a powerful and flexible set of classes and interfaces for managing groups of objects. `ArrayList` is a versatile and commonly used class within this framework. Understanding how to use `ArrayList` and other collections, as well as the algorithms provided by the `Collections` class, is crucial for effective Java programming.

docs/java/arrays-and-collections/arrays-in-java.md

Lines changed: 158 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,161 @@ sidebar_label: Arrays in Java
55
sidebar_position: 1
66
tags: [java, arrays, programming, java arrays]
77
description: In this tutorial, we will learn about arrays in Java. We will learn about what arrays are, how to declare and initialize arrays, how to access elements in an array, and how to use arrays in Java.
8-
---
8+
---
9+
10+
# Arrays in Java
11+
12+
## Introduction
13+
14+
Arrays in Java are used to store multiple values of the same type in a single variable, instead of declaring separate variables for each value. Arrays are a fundamental data structure that can help in organizing and managing data efficiently.
15+
16+
## Array Declaration
17+
18+
### Syntax
19+
20+
To declare an array, specify the data type followed by square brackets and the array name.
21+
22+
```java
23+
dataType[] arrayName;
24+
```
25+
26+
### Example
27+
28+
```java
29+
int[] numbers;
30+
String[] names;
31+
```
32+
33+
## Array Initialization
34+
35+
### Static Initialization
36+
37+
You can initialize an array at the time of declaration with a set of values.
38+
39+
```java
40+
int[] numbers = {1, 2, 3, 4, 5};
41+
String[] names = {"Alice", "Bob", "Charlie"};
42+
```
43+
44+
### Dynamic Initialization
45+
46+
You can also allocate memory for the array using the `new` keyword and then assign values to the array elements.
47+
48+
```java
49+
int[] numbers = new int[5]; // Array of 5 integers
50+
numbers[0] = 1;
51+
numbers[1] = 2;
52+
numbers[2] = 3;
53+
numbers[3] = 4;
54+
numbers[4] = 5;
55+
```
56+
57+
## Accessing Array Elements
58+
59+
Array elements are accessed using their index, which starts from 0.
60+
61+
### Example
62+
63+
```java
64+
int[] numbers = {1, 2, 3, 4, 5};
65+
System.out.println(numbers[0]); // Outputs: 1
66+
System.out.println(numbers[4]); // Outputs: 5
67+
```
68+
69+
## Looping Through Arrays
70+
71+
### For Loop
72+
73+
You can use a `for` loop to iterate through all the elements of an array.
74+
75+
```java
76+
int[] numbers = {1, 2, 3, 4, 5};
77+
for (int i = 0; i < numbers.length; i++) {
78+
System.out.println(numbers[i]);
79+
}
80+
```
81+
82+
### Enhanced For Loop (For-Each Loop)
83+
84+
The enhanced `for` loop provides a simpler way to iterate through the elements of an array.
85+
86+
```java
87+
int[] numbers = {1, 2, 3, 4, 5};
88+
for (int number : numbers) {
89+
System.out.println(number);
90+
}
91+
```
92+
93+
## Multi-Dimensional Arrays
94+
95+
Java supports multi-dimensional arrays, which are arrays of arrays.
96+
97+
### Two-Dimensional Array
98+
99+
#### Declaration and Initialization
100+
101+
```java
102+
int[][] matrix = {
103+
{1, 2, 3},
104+
{4, 5, 6},
105+
{7, 8, 9}
106+
};
107+
```
108+
109+
#### Accessing Elements
110+
111+
```java
112+
System.out.println(matrix[0][0]); // Outputs: 1
113+
System.out.println(matrix[2][2]); // Outputs: 9
114+
```
115+
116+
#### Looping Through a Two-Dimensional Array
117+
118+
```java
119+
for (int i = 0; i < matrix.length; i++) {
120+
for (int j = 0; j < matrix[i].length; j++) {
121+
System.out.print(matrix[i][j] + " ");
122+
}
123+
System.out.println();
124+
}
125+
```
126+
127+
### Example: Matrix Addition
128+
129+
```java
130+
public class MatrixAddition {
131+
public static void main(String[] args) {
132+
int[][] matrix1 = {
133+
{1, 2, 3},
134+
{4, 5, 6},
135+
{7, 8, 9}
136+
};
137+
138+
int[][] matrix2 = {
139+
{9, 8, 7},
140+
{6, 5, 4},
141+
{3, 2, 1}
142+
};
143+
144+
int[][] sum = new int[3][3];
145+
146+
for (int i = 0; i < matrix1.length; i++) {
147+
for (int j = 0; j < matrix1[i].length; j++) {
148+
sum[i][j] = matrix1[i][j] + matrix2[i][j];
149+
}
150+
}
151+
152+
// Display the result
153+
for (int[] row : sum) {
154+
for (int element : row) {
155+
System.out.print(element + " ");
156+
}
157+
System.out.println();
158+
}
159+
}
160+
}
161+
```
162+
163+
## Conclusion
164+
165+
Arrays are a powerful and essential feature in Java, allowing you to store and manage collections of data efficiently. Understanding how to declare, initialize, and manipulate arrays, as well as how to use multi-dimensional arrays, is crucial for effective Java programming.

0 commit comments

Comments
 (0)