• Post Reply Bookmark Topic Watch Topic
  • New Topic

Massive values to console  RSS feed

 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want that all massive values to be printed to console.
Of course I can initialize values this way:   mas [0] = 10;
But that would be very time consuming to set values for 100 massive elements.

So what I want?
First value is 10, second is 11, 3rd is 11, and so on every value grows by +1...







I have attached console result.
As you can see, second 'System.out' prints mas [0] = 0. I expected to print it 10.
console.png
[Thumbnail for console.png]
 
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bob john wrote:
As you can see, second 'System.out' prints mas [0] = 0. I expected to print it 10.


At no time, in your code, do you set any element of the "mas" array. So, why would you expect it to be anything else but zero?

Henry
 
bob john
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:

At no time, in your code, do you set any element of the "mas" array.

Henry


How to?
I have copied full code here.
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at the Java™ Tutorials about arrays.

You must initialise all elements of an array unless you want them to have the default values. In the case of an array of reference types, the default is null, which is potentially dangerous. Find out about how to initialise arrays with array initialisers, which can avoid default values.
 
bob john
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:

You must initialise all elements of an array unless you want them to have the default values. In the case of an array of reference types, the default is null, which is potentially dangerous. Find out about how to initialise arrays with array initialisers, which can avoid default values.


I understand that default value is null.
But I want set values via loop, as you can see in example.


I can type, but this is not the purpose.
I want to set massive element values via loop.
Also every next value to be bigger by +1.

So in the result i should have a massive with elements collection: 10, 11, 12, 13, 14, 15 ,16 ,17 ,18 ,19 ,20.
And If I type
Console shouldnt show '0'.
 
Sheriff
Posts: 4931
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bob john wrote:in the result i should have a massive with elements collection: 10, 11, 12, 13, 14, 15 ,16 ,17 ,18 ,19 ,20.

And If I type

System.out.println ( mas [4] ) ;

Console shouldnt show '0'.

What I'm missing here? You say your array starting to count from index zero begins with elements 10, 11, 12... N-1.
And when you print out fifth element, which is under index 4 you'd expect it to print 0? 

Can you describe once again the problem?
1. I want an array of size 10.
2. Elements in an array should start from 10, 11..., 20.
3. When I print ... I want... ?

I don't seem to understand the task, that is the biggest problem to me at the moment.
 
Liutauras Vilda
Sheriff
Posts: 4931
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bob john wrote:I understand that default value is null.
Your current understanding is incorrect having in mind that you're talking about integers. That is true about the objects.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bob john wrote:. . . I understand that default value is null.
That is incorrect for your array; the default value is 0.

But I want set values via loop, . . .
That is not at all difficult, but now that Java8 is here, consider a Stream, which is an object which does the iteration for you. Creating an array with consecutive values is quite easy with a Stream, but you want a Stream of ints, which is called an IntStream.The range() method creates a Stream going from 10 to 19 (there is another method which will go from 10 to 20), and it can then be told to create an int[] with toArray(). The Arrays#toString() method converts the array to a form you can understand.
If you want a loop, that is quite easy:-You will have to make slight changes to that if you want your numbers ascending.
 
Liutauras Vilda
Sheriff
Posts: 4931
334
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One more thing: such code formatting and indentation not worth having in any circumstances. Maybe just to show how code should not be written.

I improved indentation and formatting, and renamed some variables. See if you can undersand it better.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:. . . Can you describe once again the problem?
1. I want an array of size 10.
2. Elements in an array should start from 10, 11..., 20.
. . .
Fencepost numbers. 10 to 20 is eleven numbers.
 
Liutauras Vilda
Sheriff
Posts: 4931
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Fencepost numbers. 10 to 20 is eleven numbers.
I quite often get that wrong, but never got it wrong during exams

And thanks for pointing that out.
 
bob john
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


1. I want an array of size 10.
2. Elements in an array should start from 10, 11..., 20.

3. I want to SET mas[]  elements values. The full code I provided, it dont SET array elements values . It just performs mathematic action.

At 24th line 'System.out shows' '0'. But I expected wanted it to show '10'.
Of course I could write at 8th line mas [0] = 10;  But it is not my intention, as I try to image how monotonous it would be If have to SET these values for +200 elements.



 
Liutauras Vilda
Sheriff
Posts: 4931
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bob john wrote:I want to SET mas[]  elements values

When you want to assign 4 to a, what you do?
a + 4
or
a = 4

?
 
bob john
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:One more thing: such code formatting and indentation not worth having in any circumstances. Maybe just to show how code should not be written.

I improved indentation and formatting, and renamed some variables. See if you can undersand it better.


Gold. Thanks!
 
bob john
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or wait...

16th line.
Console still shows 0.

If I type at 16th line " ; It should show 10 (console).
If ; it should show 11
 
Liutauras Vilda
Sheriff
Posts: 4931
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell showed you already how to do it properly.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bob john wrote:Or wait...


Liutauras code changes was done so that you can read it better -- and see why, obviously, it is not doing what you think it is doing. 

Henry
 
Liutauras Vilda
Sheriff
Posts: 4931
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

It doesn't make 'a' to be 3. a is still 0.
If you want 'a' to be 3, you need to do:
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bob john wrote:I want that all massive values to be printed to console.
Of course I can initialize values this way:   mas [0] = 10;
But that would be very time consuming to set values for 100 massive elements.
So what I want?
First value is 10, second is 11, 3rd is 11, and so on every value grows by +1...

Bob, can I suggest something to you?

Programming is a precise discipline, and it requires YOU to be precise, both when you're programming and when you're describing a problem to other people.

So, just taking your statement:
Line 1: What is "massive"? It means absolutely nothing to us; and furthermore, you called your array 'mas', not 'massive'.
Line 2: 'mas [0] = 10' does NOT set "values"; it sets a value - specifically, the first one in 'mas'.
Line 3: Where does 100 come from? In your program 'mas' is only 5 elements long.

Now I understand what you're trying to say/do, and I suspect that English isn't your native language, but it's only at line 5 that you describe what you actually want.

You've also compounded your problem by using a "for-each" loop. These can be very useful, but in your case the value of the index is quite important, so consider using the classical method of going through an array instead:HIH

Winston
 
bob john
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This time I created ArrayList, not massive.



The code itself is very fine for me. Problem comes at 20th line.
I want the console to show 1st ArrayList element, but compilator marks as mistake.
However if I

It has no errors, shows full arrayList collection, but I want only first.
 
Liutauras Vilda
Sheriff
Posts: 4931
334
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Save this website to your browsers bookmars, https://docs.oracle.com/javase/8/docs/api/

Press CTRL + F, type in ArrayList and click once you find it. Then search for a method you need to get an element. I gave you a hint already in the previuo sentence
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!