Welcome to Dream.In.Code
Become a Java Expert!

Join 150,194 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,052 people online right now. Registration is fast and FREE... Join Now!




Arrays

 
Reply to this topicStart new topic

Arrays

soccerizzy26
23 Sep, 2008 - 07:08 AM
Post #1

D.I.C Head
**

Joined: 23 Jan, 2008
Posts: 65

I can't seem to write my code so it will compile, if I even have the right code written.
What i'm trying to do is make an array and then use a for loop to make each value equal to the length.
For example if you were to type in
System.out.println(intArray[3]) that the output would be 3

CODE

public class Assignment2
    {
        public static void main(String[] args)
        {
            int[] intArray = new int[10];
            for (int i=0; i<=9; i++)
            {
                intArray[i] = "0" + i
            }
        }
    }

User is offlineProfile CardPM
+Quote Post

Gloin
RE: Arrays
23 Sep, 2008 - 07:17 AM
Post #2

Expert Schmexpert...
Group Icon

Joined: 4 Aug, 2008
Posts: 934



Thanked: 54 times
My Contributions
You're not too far off

public class Assignment2
{
public static void main(String[] args)
{
int[] intArray = new int[10];
for (int i=0; i < intArray.length; i++)
{
intArray[i] = i;
}
}
}


User is offlineProfile CardPM
+Quote Post

bbq
RE: Arrays
23 Sep, 2008 - 07:31 AM
Post #3

D.I.C Head
Group Icon

Joined: 15 May, 2008
Posts: 200



Thanked: 17 times
Dream Kudos: 50
My Contributions
java

public class Assignment2
{
public static void main(String[] args)
{
int[] intArray = new int[10];
for (int i=0; i < intArray.length; i++)
{
intArray[i] = i;
}
}
}


Notice how Gloin also uses the intArray.length instead of your <=9, try and stick to using this to avoid index out of bounds errors... In your original code

You use this statement (not a statement)
java

intArray[i] = "0" + i


Just to explain you are trying to add a strings "0" and an integer i together and store them in an array of type int, this won't work. Just a quick question on this, are you simply trying to store the 10 values into the array being
0 1 2 3 4 5 6 7 8 9
or are you trying to do something a little different ?
User is offlineProfile CardPM
+Quote Post

soccerizzy26
RE: Arrays
23 Sep, 2008 - 07:41 AM
Post #4

D.I.C Head
**

Joined: 23 Jan, 2008
Posts: 65

Yes I am just trying to store the 10 values in the array to be
0 1 2 3 4 5 6 7 8 9

and when I use the program it prints out differently then how i expected.

CODE

public class Assignment2
    {
        public static void main(String[] args)
        {
            int[] intArray = new int[10];
            for (int i=0; i<=intArray.length; i++)
            {
            intArray[i] = i;  
            System.out.println(intArray[2]);
            }
        }
    }


In my terminal the output is
0 0 2 2 2 2 2 2 2 2 , so I understand that the output will print 0 until it actually gets to the number I try to call and will keep printing until it gets to the 10th element. How do I make it so that the output will only be the number I want.

Also instead of
CODE
for (int i=0; i<=intArray.length; i++)

shouldnt it be
CODE
for (int i=0; i<=intArray.length-1; i++)



This post has been edited by soccerizzy26: 23 Sep, 2008 - 07:46 AM
User is offlineProfile CardPM
+Quote Post

Gloin
RE: Arrays
23 Sep, 2008 - 11:09 AM
Post #5

Expert Schmexpert...
Group Icon

Joined: 4 Aug, 2008
Posts: 934



Thanked: 54 times
My Contributions
To answer both your questions (were they two?)

First
When you create an integer array of size, let's say 10. All posts in the array are auto-initialized to 0.
In your code, you choose to print out the value stored in intArray[2] for each iteration of the for-loop and during the first two iterations of the loop, the variable intArray[2] contains the value 0 since you have not yet set the value to 2.

Second, this

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

will cause an indexoutofboundsexception (or something, can't remember the exact name)

intArray.length = 10 but the largest index of the array is 9 so the loop ought to be

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

which will loop as long as i < 10, I.e at most 9.

This post has been edited by Gloin: 23 Sep, 2008 - 11:14 AM
User is offlineProfile CardPM
+Quote Post

bbq
RE: Arrays
23 Sep, 2008 - 10:02 PM
Post #6

D.I.C Head
Group Icon

Joined: 15 May, 2008
Posts: 200



Thanked: 17 times
Dream Kudos: 50
My Contributions
java

public class Assignment2
{
public static void main(String[] args)
{
int[] intArray = new int[10];
for (int i=0; i<=intArray.length; i++)
{
intArray[i] = i;
System.out.println(intArray[2]);
}
}
}


Very close, however your line

java

System.out.println(intArray[2]);


Causes the program to print out the value stored in position 2 over and over... so you need to get that out of there and and print it out as
java

System.out.println(intArray[i]);


This should work cool.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 04:29AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month