Showing posts with label array. Show all posts
Showing posts with label array. Show all posts

20 December 2013

Useful link: Why array are zero-indexed?

Curiosity can be evil.You sometimes want know ..why?  Why array are zero-indexed? Is it improve my life?No. However ,I was still curious. Thanks to Mike Hoye's weblog  .I know the answer .
" So: the technical reason we started counting arrays at zero is that in the mid-1960′s, you could shave a few cycles off of a program’s compilation time on an IBM 7094. The social reason is that we had to save every cycle we could, because if the job didn’t finish fast it might not finish at all and you never know when you’re getting bumped off the hardware because the President of IBM just called and fuck your thesis, it’s yacht-racing time."Mike Hoye
I recommend to read his amazing article about it:
 http://exple.tive.org/blarg/2013/10/22/citation-needed/

13 January 2012

How to create meaninful toString() from Array in java ??

Reminder for myself


Normal array when you use method toString() produce nice but useless "memory adress gibberish".However if you want  meaningful representation of array as string then use this method (from Arrays class):

Arrays.toString(myAwesomeArray)  - where myAwesomeArray is  .... your array ;P

Simple and useful,but i discover that that it use more memory,so it can happen that will see this error for reallly really huge array:
Java heap space
java.lang.OutOfMemoryError: Java heap space

4 November 2011

How to compare if 2 arrays has equal members?

This is note to myself (because I always forgot about this very easy way to compare Arrays)

How to check is 2 arrays has equal members in the same order?
Arrays.equals(a1, a2);

for example:
boolean isEqual = Arrays.equals(a1, a2);

for example 2:
if(Arrays.equals(a1, a2)){
  System.out.print("They are equal.");
}else{
  System.out.print("They are NOT equal.Really.they are different.I know is hard to live with that,but true is they are different.");
}

From Oracle JavaDoc:
"The two arrays are considered equal if both arrays contain the same number of elements, and all corresponding pairs of elements in the two arrays are equal.(...) In other words, the two arrays are equal if they contain the same elements in the same order. Also, two array references are considered equal if both are null"