Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Java
  3. Question
Java

How exactly Java indexing works in arrays?

Asked by Murakami Tanaka Oct 11, 2022 1.1K views 1 answer
Share

About this question

 I only know that index is faster but don't know why it is faster.

Suppose I have an array int[] a = {2,3,6,7}. Then I will try to find the element at a[3] and the speed of this will be O(1). Why? How will it know that 3 is placed after 2 boxes? So for more clarification of my question here is the structure of the array I'm expecting.

index   values 

 [0] ->  [2]

 [1] ->  [3]

 [2] ->  [6]

 [3] ->  [7]

Why will a[3] go directly to 7?

HashTable

Same confusion I have for the HashTable:

 hash   values 

 [7nsh] ->  [2]

 [j2ns] ->  [3]

 [9sjm] ->  [6]

 [an5k] ->  [7]

If I search for the value from a hash function getValue(6), it will generate the same key 9sjm but how does it know that the key is placed on the third number? Same as how it could be O(1)?


Your answer

1 Answer

More Java discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest Java Blogs

Guides, tips and career advice on Java from JanBask experts.