Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Single Dimensional Array Help!

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Single Dimensional Array Help!

    This is what I'm supposed to do:

    A Java program using single dimensional array. Have a list of 10 numbers and fetch a value based on the index.

    The second part is not very clear, so I ended up with my code saying:

    class SingleDimensionalArray
    {
    public static void main(String args[])
    {
    int a[]=new a[10];
    a[]={1,2,3,4,5,6,7,8,9,10};
    }
    }

    What's wrong with my code?And/or what did I leave out?


  2. #2
    Member
    Join Date
    Feb 2011
    Posts
    55
    My Mood
    Tolerant
    Thanks
    1
    Thanked 16 Times in 15 Posts

    Default Re: Single Dimensional Array Help!

    There is nothing wrong with this program, but the how the values should be fetched is vague. Should the program ask the user to enter an index for the number to be returned, or should a method be created to fetch the value? You need to ask for clarification.

    What you currently have is a single dimensional array 'a' that has 10 numbers that can be accessed through a[0] to a[9]

  3. #3
    Junior Member
    Join Date
    Mar 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Single Dimensional Array Help!

    What is an index?

  4. #4
    Junior Member
    Join Date
    Mar 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Single Dimensional Array Help!

    And when I compile that program it comes up with 4 errors:

    SingleDimensionalArray.java:6: not a statement
    a[]=(1,2,3,4,5,6,7,8,9,19);
    ^
    SingleDimensionalArray.java:6: ';' expected
    a[]=(1,2,3,4,5,6,7,8,9,10);
    ^
    SingleDimensionalArray.java:6: not a statement
    a[]=(1,2,3,4,5,6,7,8,9,10);
    ^
    SingleDimensionalArray.java:6: ';' expected
    a[]=(1,2,3,4,5,6,7,8,9,10);
    ^

  5. #5
    Junior Member
    Join Date
    Mar 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Single Dimensional Array Help!

    Okay, I re wrote it and now I only have 1 error:

    class SingleDimensionalArray
    {
    public static void main(String args[])
    {
    int a[]= new a[10];
    a[0] = 1;
    a[1] = 2;
    a[2] = 3;
    a[3] = 4;
    a[4] = 5;
    a[5] = 6;
    a[6] = 7;
    a[7] = 8;
    a[8] = 9;
    a[9] = 10;
    System.out.println(a[0]);
    System.out.println(a[1]);
    System.out.println(a[2]);
    System.out.println(a[3]);
    System.out.println(a[4]);
    System.out.println(a[5]);
    System.out.println(a[6]);
    System.out.println(a[7]);
    System.out.println(a[8]);
    System.out.println(a[9]);
    }
    }

  6. #6
    Member vanDarg's Avatar
    Join Date
    Jan 2011
    Location
    Chicago
    Posts
    65
    My Mood
    Mellow
    Thanks
    1
    Thanked 7 Times in 7 Posts

    Default Re: Single Dimensional Array Help!

    Please put your code in tags:
    HTML Code:
    [highlight=Java] [/highlight]
    What is the error you are getting?
    An index is a reference number for a value (in an array). For instance, an index in your array:

    The index '2' refers to the integer value 3.
    Last edited by vanDarg; March 15th, 2011 at 12:44 AM.
    "Everything should be made as simple as possible, but not simpler."
    Asking Questions for Dummies | The Java Tutorials | Java Coding Styling Guide

  7. #7
    Member
    Join Date
    Mar 2011
    Location
    Earth!
    Posts
    77
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: Single Dimensional Array Help!

    A few things to note. [] should be after the type, not the variable name ("int[] name", not "int name[]"). "new" should be followed by the type and not the variable name ("new int[10]", not "new a[10]"). It seems like you want to create the array and give it all its values in two lines. You can do it in one line, like this:
    int[] a=new int[]{1,2,3,4,5,6,7,8,9,10};
    Hope this clears things up .
    Last edited by Kerr; March 15th, 2011 at 12:08 PM.

Similar Threads

  1. Two-Dimensional Array and Loops
    By astrojunk in forum Java Theory & Questions
    Replies: 2
    Last Post: February 11th, 2011, 07:18 AM
  2. Finding the length of a two dimensional array
    By petemyster in forum Java Theory & Questions
    Replies: 2
    Last Post: December 12th, 2010, 10:21 PM
  3. How to write 2 dimensional array of float numbers to binary file?
    By Ghuynh in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: June 17th, 2010, 04:26 PM
  4. 2 dimensional array alternative ???
    By zeeshanmirza in forum Java SE APIs
    Replies: 1
    Last Post: February 23rd, 2010, 06:18 PM
  5. Multi Dimensional Array help NEEDED URGENT
    By bonjovi4u in forum Loops & Control Statements
    Replies: 5
    Last Post: February 13th, 2010, 12:44 AM