• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Arrays problem

 
Greenhorn
Posts: 14
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a problem with this, so when you run this you don't get an array list....result comes in column...why?

import java.util.ArrayList;
public class Arrays {

public static void main(String[] args) {
// TODO Auto-generated method stub
int value = 7;
int [] values;
values = new int[10];
System.out.println(values[0]);

values[0] = 1;
values[1] = 2;
values[2] = 3;
values[3] = 4;
values[4] = 5;
values[5] = 6;
values[6] = 7;
values[7] = 8;
values[8] = 9;
values[9] = 10;
System.out.println(values[0]);
System.out.println(values[1]);
System.out.println(values[2]);
System.out.println(values[3]);
System.out.println(values[4]);
System.out.println(values[5]);
System.out.println(values[6]);
System.out.println(values[7]);
System.out.println(values[8]);
System.out.println(values[9]);
//System.out.println(values[10]);

}

}


 
Saloon Keeper
Posts: 9742
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
System.out.println() adds a new line character(s) each time resulting in a column. If you want a list use System.out.print(values[0]+" "). As you can see you'll need to insert a space yourself, otherwise all the prints will run together.
 
Janko Livic
Greenhorn
Posts: 14
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:System.out.println() adds a new line character(s) each time resulting in a column. If you want a list use System.out.print()+" ". As you can see you'll need to insert a space yourself, otherwise all the prints will run together.



But what is use of arrays if i need to do "..print"?
Then i need no array parts of code right?
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Janko Livic wrote:But what is use of arrays if i need to do "..print"?


Have a look at Arrays.toString().

Winston
 
Carey Brown
Saloon Keeper
Posts: 9742
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Usually, to print an array you put it inside a loop which steps through the array.
 
Greenhorn
Posts: 11
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you can solve that with automatic pass with FOR loop
 
Janko Livic
Greenhorn
Posts: 14
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ilija Erceg wrote:I think you can solve that with automatic pass with FOR loop



I agree.
 
Ilija Erceg
Greenhorn
Posts: 11
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where are you from,maybe i can help on your native language ?
 
Janko Livic
Greenhorn
Posts: 14
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ilija Erceg wrote:Where are you from,maybe i can help on your native language ?



Croatia, every advice is welcome!!
 
Ranch Hand
Posts: 386
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Winston's way is the one you're looking for.

But to answer your question, it's creating a list because you are calling each individual element line by line (values[0], values[1], values[2] etc). System.out.println(array[x]) is what is causing each item to be on a new line (println)

oh, and you don't need that import statment, but you'll need something similar for Winton's way (tm)

 
My, my, aren't you a big fella. Here, have a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic