0% found this document useful (0 votes)
2 views4 pages

Java Arrays and ArrayList Guide

The document provides notes on Java Arrays and ArrayLists, highlighting key features such as fixed size for arrays and dynamic size for ArrayLists. It includes examples of array declarations, multi-dimensional arrays, jagged arrays, and methods for passing arrays. Additionally, it covers ArrayList operations, including adding elements, iterating through the list, and common methods available.

Uploaded by

laila.alheilat
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)
2 views4 pages

Java Arrays and ArrayList Guide

The document provides notes on Java Arrays and ArrayLists, highlighting key features such as fixed size for arrays and dynamic size for ArrayLists. It includes examples of array declarations, multi-dimensional arrays, jagged arrays, and methods for passing arrays. Additionally, it covers ArrayList operations, including adding elements, iterating through the list, and common methods available.

Uploaded by

laila.alheilat
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

‫( محتوى الصور‬Java Arrays & ArrayList Notes)

------------------------------------------
‫الصفحة األولى‬
------------------------------------------

# Arrays
* Array ‫ لها حجم ثابت‬Fix Size
* Array = Direct Access ‫الوصول المباشر‬

‫مثال على مصفوفة‬:


[8, 5, 2, 3, 4, 5, 6, 12]
length = 8
First Index = 0
Last Index = length - 1

------------------------------------------
Declaration
------------------------------------------
int[] c = new int[12];
int c[] = new int[12];
int grades[] = {1,2,3,4,5};

c[0] = 3;
c[1] = 23;

double sum = 0;
for (int i=0; i < [Link]; i++) {
sum = sum + grades[i];
}
[Link]("Average = " + sum/[Link]);

------------------------------------------
Enhanced For
------------------------------------------
double[] myList = {1.9, 2.7, 5.9, 3};
for(double e : myList) {
[Link](e);
}

------------------------------------------
Multi-Dimensional Array
------------------------------------------
datatype [][] name = new datatype[size1][size2];

‫مثال‬:
double[][] x = new double[10][20];
200 = ‫عدد العناصر‬

‫مثال آخر‬:
int[][] I = new int[3][5];

------------------------------------------
2D Array Example
------------------------------------------
int[][] arr = { {1,2,5}, {3,4,5} };

for(int i=0; i < [Link]; i++) {


for(int j=0; j < arr[i].length; j++) {
[Link]("arr[" + i + "][" + j + "] = " + arr[i][j]);
}
}

------------------------------------------
Jagged Array (‫)عدد األعمدة متغير‬
------------------------------------------
int arr[][] = new int[3][];
arr[0] = new int[3];
arr[1] = new int[4];
arr[2] = new int[2];

int count = 0;
for(int i=0; i < [Link]; i++) {
for(int j=0; j < arr[i].length; j++) {
arr[i][j] = count++;
[Link]("arr[" + i + "][" + j + "] = " + arr[i][j]);
}
}

------------------------------------------
‫تغيير الحجم‬
------------------------------------------
int[][] A = new int[5][6];
A = new int[10][6];
------------------------------------------
Passing Array to Method
------------------------------------------
public static void printArray(int[] array) {
for(int i=0; i < [Link]; i++) {
[Link](array[i]);
}
}

main:
printArray(new int[]{3,2,4,5});

------------------------------------------
‫الصفحة الثانية‬
------------------------------------------

# ArrayList

• ‫ أسهل وأفضل من الـ‬Array ‫ ألنها‬dynamic (‫)غير ثابتة الحجم‬


ArrayList<String> cars = new ArrayList<String>();
[Link]("BMW");
[Link]("Ford");
[Link]("Mazda");

[Link]("Cars = " + cars);

------------------------------------------
‫ استخدام‬for loop ‫ مع‬ArrayList
------------------------------------------
for(int i=0; i < [Link](); i++) {
[Link]([Link](i));
}

------------------------------------------
‫ استخدام‬For Each
------------------------------------------
for(String car : cars) {
[Link](car);
}

------------------------------------------
Generic vs Non-Generic
------------------------------------------
ArrayList L = new ArrayList();
[Link](1234);
[Link]("Ahmad");
[Link](23.4);
[Link](L);

------------------------------------------
Iterator Interface
------------------------------------------
Iterator t = [Link]();
while([Link]()) {
[Link]([Link]());
}

------------------------------------------
‫ طرق‬ArrayList
------------------------------------------
1. add()
2. size()
3. get()
4. remove()
5. clear()
(‫)…وغيرها‬

------------------------------------------

You might also like