Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Arrays problem  RSS feed

 
Janko Livic
Greenhorn
Posts: 14
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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]);

}

}


 
Carey Brown
Bartender
Posts: 3024
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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?
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
Bartender
Posts: 3024
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Usually, to print an array you put it inside a loop which steps through the array.
 
Ilija Erceg
Greenhorn
Posts: 11
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • 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
  • 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
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • 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
  • 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!!
 
nick woodward
Ranch Hand
Posts: 372
11
  • Mark post as helpful
  • send pies
  • 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)

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!