Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9142483
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:47:36+00:00 2026-06-17T09:47:36+00:00

I have written the code below to check the elements of array. I got

  • 0

I have written the code below to check the elements of array. I got the required answer. I just want to know what changes should I do so that it gives me the output as 1, 2, 3, 4, 5.

Also when I write a[] instead of b[], the code gives me the error a is already defined in main(java.lang.String[]). Could you please help me?

class ArrayDemo15
{
    public static void main(String a[])
    {
        int b[]=new int[5];
        for(int i=0;i<5;i++)
        {
            System.out.println(b[i]+""+);
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T09:47:37+00:00Added an answer on June 17, 2026 at 9:47 am

    “I just want to know what changes should I do so that it gives me the output as 1, 2, 3, 4, 5“

    If you just want to print those numbers then you dont need array at all. Just use for loop like

    int counter=1;
    for (counter=1; counter<=4; counter++)
        System.out.print(counter+", ");
    //after loop print last element without comma
    System.out.println(counter);
    

    But if you insist in improving your code and using array continue reading this answer…

    Right now your code gives compilation error in System.out.println(b[i]+""+); since + in this case is two argument operator and you give it only one argument. Change it to something like

    System.out.print(b[i]+", ");
    

    I used print instead println since you don’t want to have new line signs between numbers.

    Also currently your array is filled with zeros, because all new arrays are filled with some default values:

    – for arrays of primitive numbers type (int, byte, double and so on) default value is 0,

    – for primitive boolean it is false,

    – and for Objects (like Strings) it is null.

    So you need to fill your array first with your values. To do that you have two options

    1. iterate over array and set every element

      for (int index = 0; index < b.length; index++) {
          b[index] = index + 1;
      }
      
    2. provide all values while creating array, like
      • int[] b1 = { 1, 2, 3, 4, 5 }; this version can be used only with reference
      • int[] b2 = new int[]{ 1, 2, 3, 4, 5 }; this version doesn’t need reference and can be used everywhere for example as argument of some method that accepts array of int like Arrays.sort(new int[]{ 5, 3, 1, 4, 2 })

    When all elements of array are set to correct values you need to print it. You can do it with built-in utility as A. R. S. pointed System.out.println(java.util.Arrays.toString(b)) or do it yourself with loops for example

    for (int i = 0; i < b.length - 1; i++) {// b.length - 1 I don't want to
        // print last element here since I don't want to add comma
        // after it
        System.out.print(b[i] + ", ");
    }
    // now it is time for last element of array
    System.out.println(b[b.length - 1]);
    //since b[]={1,2,3,4,5} b.length=5 so it will print b[4] -> 5
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written below code to check for blank value in my textbox but
Hi I have written below code function unique(th){ var obj = {}; for(var i
I am trying to fetch wcf service from jquery. I have written below code
I have written grammar for a language (sample code below) //this is a procedure
I have written a very simple WCF service, that worked fine (code below), then
I have written a MapReduce program, code is below: import java.io.IOException; import java.util.Iterator; import
I have written a code for avl which is giving me the below error
I have written some code to check two dates, a start date and an
I have written this code. Its not working but with this you should get
I have written the following code to check if a tree is a Binary

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.