Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
Core Java and Java 8
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Lokesh Singh
Core Java and Java 8
Commits
6b9f387e
Unverified
Commit
6b9f387e
authored
May 08, 2022
by
Lokesh Singh
Committed by
GitHub
May 08, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update readme.md
parent
1b08296a
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
74 additions
and
0 deletions
+74
-0
readme.md
Array/readme.md
+74
-0
No files found.
Array/readme.md
View file @
6b9f387e
...
...
@@ -61,4 +61,78 @@ for(int i = 0; i<intArray.length; i++) {
}
```
## Array of Object
An array of objects is created like an array of primitive type data items in the following way.
```
java
Student
[]
arr
=
new
int
[
7
];
// student is user defined class
```
```
java
// Java program to illustrate creating
// an array of objects
class
Student
{
public
int
roll_no
;
public
String
name
;
Student
(
int
roll_no
,
String
name
)
{
this
.
roll_no
=
roll_no
;
this
.
name
=
name
;
}
}
// Elements of the array are objects of a class Student.
public
class
GFG
{
public
static
void
main
(
String
[]
args
)
{
// declares an Array of integers.
Student
[]
arr
;
// allocating memory for 5 objects of type Student.
arr
=
new
Student
[
5
];
// initialize the first elements of the array
arr
[
0
]
=
new
Student
(
1
,
"aman"
);
// initialize the second elements of the array
arr
[
1
]
=
new
Student
(
2
,
"vaibhav"
);
// so on...
arr
[
2
]
=
new
Student
(
3
,
"shikar"
);
arr
[
3
]
=
new
Student
(
4
,
"dharmesh"
);
arr
[
4
]
=
new
Student
(
5
,
"mohit"
);
// accessing the elements of the specified array
for
(
int
i
=
0
;
i
<
arr
.
length
;
i
++)
System
.
out
.
println
(
"Element at "
+
i
+
" : "
+
arr
[
i
].
roll_no
+
" "
+
arr
[
i
].
name
);
}
}
```
### What happens if we try to access elements outside the array size?
JVM throws ArrayIndexOutOfBoundsException to indicate that the array has been accessed with an illegal index.
```
java
package
javaLearning
;
public
class
printArray
{
public
static
void
main
(
String
[]
args
)
{
int
[]
arr
=
new
int
[
2
];
arr
[
0
]
=
10
;
arr
[
1
]
=
20
;
for
(
int
i
=
0
;
i
<=
arr
.
length
;
i
++)
System
.
out
.
println
(
arr
[
i
]);
}
}
error:
10
20
Exception
in
thread
"main"
java
.
lang
.
ArrayIndexOutOfBoundsException
:
Index
2
out
of
bounds
for
length
2
at
javaLearning
/
javaLearning
.
printArray
.
main
(
printArray
.
java
:
11
)
```
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment