9 Replies - 3289 Views - Last Post: 24 May 2011 - 02:24 AM Rate Topic: -----

#1 alpha_x  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 55
  • Joined: 03-May 11

Printing out array?

Posted 24 May 2011 - 12:32 AM

So I have a problem where in I need to print out 10 digits but my code is only printing out 9? Any ideas?
int[] array = { 1,2, 3, 4, 5, 6, 7, 8, 9, 10 };
for (int i = 1; i < 10; i++)
{
System.out.println(array[i]);
}


I have no idea why this isn't working, any ideas?
Is This A Good Question/Topic? 0
  • +

Replies To: Printing out array?

#2 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1407
  • View blog
  • Posts: 3,123
  • Joined: 05-April 11

Re: Printing out array?

Posted 24 May 2011 - 12:33 AM

your array starts at the index 0, not 1
Was This Post Helpful? 1
  • +
  • -

#3 v0rtex  Icon User is offline

  • Caffeine: db "Never Enough!"
  • member icon

Reputation: 223
  • View blog
  • Posts: 773
  • Joined: 02-June 10

Re: Printing out array?

Posted 24 May 2011 - 12:33 AM

Java's arrays are off-indexed so initialize i at 0 not 1, its looping from 1-9 when it should be looping from 0-9
EDIT: Too late! :D

This post has been edited by v0rtex: 24 May 2011 - 12:33 AM

Was This Post Helpful? 1
  • +
  • -

#4 alpha_x  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 55
  • Joined: 03-May 11

Re: Printing out array?

Posted 24 May 2011 - 12:35 AM

Wow that was fast, thanks a lot, this worked :)
for (int i = 0; i < 10; i++) 
{
System.out.println(i+1); //to get 1-10 and not 0-9?
}



Output:
1
2
3
4
5
6
7
8
9
10


Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Printing out array?

Posted 24 May 2011 - 12:40 AM

Wont work if you array contains something else than 1,2,3,4,5,6 :)

You are printing i you should print what is in array[i] starting with i == 0

for(int i = 0; i < array.length; ++i)
  System.out.println("array[" + i + "] = " + array[i]);


This post has been edited by pbl: 24 May 2011 - 12:43 AM

Was This Post Helpful? 0
  • +
  • -

#6 Tarkenfire  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 115
  • View blog
  • Posts: 314
  • Joined: 04-May 11

Re: Printing out array?

Posted 24 May 2011 - 12:44 AM

View Postpbl, on 24 May 2011 - 12:40 AM, said:

Wont work if you array contains something else than 1,2,3,4,5,6 :)

You are printing i you should print what is in array[i] starting with i == 0

for(int i = 0; i < array.length; ++i)
  System.out.println(array[i]);




Serious question, was ++i just a typo or is it a java thing that I don't know?
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Printing out array?

Posted 24 May 2011 - 12:50 AM

View PostTarkenfire, on 24 May 2011 - 03:44 AM, said:

Serious question, was ++i just a typo or is it a java thing that I don't know?

In this context
++i
and
i++
are equivalent.
On some processors (and on many RISC architecture) ++i might be executed in one or two less cycles than i++. So performance oriented programmers will always use ++i or --i version when they are equivalent to the i++ and i-- version.

a[i++] = 0; is not equivalent to a[++i] = 0;
but as increment/decrement of a for() loop it does not make a difference
Was This Post Helpful? 1
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Printing out array?

Posted 24 May 2011 - 02:19 AM

Actually, if you really want to look like a pro :)

Most of the times, when you have to access an array in reverse order people code like this:

for(int i = array.length - 1; i >= 0; i--) {



now you just learned that this might be more efficient (at least in C/C++)

for(int i = array.length - 1; i >= 0; --i) {



People are used to the for() loop with 3 arguments. But the mos efficient way is like this

for(int i = array.length; --i >= 0; ) {


If you look at the code of the API you will see that the folks from Java Engineering usally use that syntax. :^:
Was This Post Helpful? 2
  • +
  • -

#9 v0rtex  Icon User is offline

  • Caffeine: db "Never Enough!"
  • member icon

Reputation: 223
  • View blog
  • Posts: 773
  • Joined: 02-June 10

Re: Printing out array?

Posted 24 May 2011 - 02:20 AM

Useful tip pbl!
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Printing out array?

Posted 24 May 2011 - 02:24 AM

Not sure it will always make a difference, especially in Java, but when I see programmers's code using --i in a for() loop I suspect them to be also experience Assembler programmers so I trust a bit more their code :) :^:.

This post has been edited by pbl: 24 May 2011 - 02:26 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1