What is ArrayIndexOutOfBoundsException ?

0 votes

What is ArrayIndexOutOfBoundsException and how can I get rid of it?

Here is a code sample that triggers the exception

String[] names= {"Ram", "Raj", "Hari"};
for(int i = 0; i<=name.length; i++){
  System.out.print(name[i] +'\n');
}
May 11, 2018 in Java by Daisy
• 8,050 points
92 views

1 answer to this question.

0 votes

If You check System.out.print(names.length);

you will get 3 i.e. names length is 3. your loop is running from 0 to 3 which should be running either 0 to 2 or 1 to 3

Solution

String[] name = {"Ram", "Raj", "Hari"};
for(int i = 0; i<names.length; i++) {
  System.out.print(names[i] +'\n');
}
answered May 11, 2018 by sharth
• 3,350 points

Related Questions In Java

+1 vote
3 answers

What is the syntax to declare and initialize an array in java?

You can use this method: String[] strs = ...READ MORE

answered Jul 25, 2018 in Java by samarth295
• 2,190 points
586 views
0 votes
2 answers

What is the syntax to initialize an array?

Rather than learning un-Official websites learn from ...READ MORE

answered Aug 2, 2018 in Java by samarth295
• 2,190 points
60 views
0 votes
2 answers

What is the difference between implements and extends?

Extends : This is used to get attributes ...READ MORE

answered Aug 3, 2018 in Java by samarth295
• 2,190 points
621 views
0 votes
1 answer

What is the difference between jdk and jre?

JRE: It stands for Java Runtime Environment. ...READ MORE

answered Apr 20, 2018 in Java by Akrati
• 3,150 points
159 views
0 votes
1 answer

How can we resolve ClassNotFoundException in Java?

Your classpath is broken. Depending on how you ...READ MORE

answered May 22, 2018 in Java by Akrati
• 3,150 points
77 views
0 votes
1 answer

How to resolve the java.net.ConnectException in Java?

If you are getting java.net.ConnectionException, it means ...READ MORE

answered Jun 4, 2018 in Java by Akrati
• 3,150 points
201 views
+1 vote
1 answer

How can we resolve java.lang.OutOfMemoryError in Java?

Work with smaller batches of HashMap Objects ...READ MORE

answered Jun 5, 2018 in Java by sharth
• 3,350 points
81 views
0 votes
1 answer

Fixing java.lang.UnsupportedClassVersionError in Java

To fix the exception/ error, you should ...READ MORE

answered Jun 7, 2018 in Java by sharth
• 3,350 points
158 views
0 votes
1 answer

What is the difference between Runnable and extends Thread

Runnable is the preferred way to create ...READ MORE

answered Apr 30, 2018 in Java by sharth
• 3,350 points
127 views
0 votes
1 answer

what are the ways in which a list can be iterated

  There are 5 ways to iterate over ...READ MORE

answered Apr 23, 2018 in Java by sharth
• 3,350 points
61 views