Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

printing arrays  RSS feed

 
Karen Guffey
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm back...

I've figured out how to create arrays, & you'd think that printing them on the screen would be a piece of cake. Not so...

You'd also think that the course textbook & course readings would be enough to answer all simple questions, like how to print, wouldn't you? You'd be wrong about that.

At any rate, I know how to print the entire array. I just can't figure out how to print a single cell/value.

What I'm supposed to do is get 10 integers & then print them backwards. The value in cell (sorry if there's a better word) 9 is printed first, the value in cell 8 is printed second, etc. My first idea was to print them out one by one, & then I thought there must be a formula for doing it. But since I can't print one at a time in ANY order...

My textbook said to use the method in my program, but that seems to do nothing with the array:



Calling the printArray method with the values 9,8,7 just prints "9 8 7".

Help...again???

Thank you in advance!
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Karen Guffey wrote:


Calling the printArray method with the values 9,8,7 just prints "9 8 7".


Have you learned loops yet? If so, can you figure out what is happening with line 20 to line 22? And once you figure out those three lines, and how it works, can you tell us, what is wrong with those lines? Meaning what is happening compared to what you want to happen, and any ideas on getting the desired result?

Henry
 
Liutauras Vilda
Marshal
Posts: 4626
316
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Please don't write such a comments in your programs as "//Ask for 10 integers" these are redundant, as by looking at the line below I can see the text "Enter 10 numbers: ".
Your code is also horibly indented, very difficult to undestand the code. Please use 4 space symbols when want to shift the code to the right hand side. Don't mix these with tabs and spaces. It can differ from os to os how these are interpreted.

So, you know how to populate an array. You got one thing left to do. Print an array backwards. Do you know how to print array elements from the beginning? Well, you should know, because the method you have it does that. What you need to work out, how to print from the last element towards the first one. Do you know what "for" loop can do?

for (int i = 0; i < array.length; i++)

please explain, what each part within loop for, separated by semicolon does?

And one more thing. Don't use variable name "list" when you use arrays.
 
Campbell Ritchie
Marshal
Posts: 55678
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are right that method call has nothing to do with the array you had. You create your 10‑element array and then on line 17 you call that method with a different array.

By the way: get yourself a text editor which supports automatic indentation. Look at our FAQ; if I use Windows® I prefer Notepad++. The reason is that your indentation is inconsistent and it is making it hard for you to read the code. Indentation is not only there so I can read your code; it is also there to make it easier for you to read your own code. And you need to avoid everything which makes life more difficult for you.
 
Karen Guffey
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know about loops, & what appears to be going on there is that the contents of the "list" array are being pulled out & printed one by one. Presumably, I should be able to type

System.out.print(list[9]);

to get it to print just the value in the 9th cell, but that doesn't work. I would think that calling the printArray method using 9,8,7 would print the values in the 9th, 8th, & 7th cells, but it doesn't.

The exact statement in my textbook is this:

"You can invoke the method by passing an array. For example, the following statement invokes the printArray method to display 3, 1, 2, 6, 4, 2.
printArray(new int[]{3, 1, 2, 6, 4, 2};"

I'd assumed that those were the number of the cells & that the value of the cells would be printed, not the exact numbers in parentheses. What would be the point of that? And then the "note" that follows the above is this:

"The preceding statement creates an array using the following syntax:
new elementType[]{value0, value1, ..., valuek];"

Does value0 not mean the value in the first cell?

 
Liutauras Vilda
Marshal
Posts: 4626
316
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You seem misunderstand the concept. Please don't use "list" when you use Arrays class. It means nothing to you at the moment, but it does to others and it could be confusing.

Lets go back to your problem.
It is array, which can hold up to 10 elements (not cells or values).

1st element starts at position 0. To access it or print it you write:
What this line does?It creates an array of length 6 and initializes it, and then passes as an argument to a method. Array itself holds elements 3, 1, 2, 6, 4, 2.
What do you want to do later with them within the method it is your choise.

Try to work out, how to print them backwards. Well, you mentioned you know how to use "for" loop, so it shouldn't be challenging.
 
Karen Guffey
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell: Can I use a text editor with an IDE? We use Netbeans for class. If not, could I copy my program from Netbeans into the text editor for indentation?

Liutaurus: Thank you! But...not challenging? Ha! Depends on how long it takes my head to absorb what's going on.

Back to Campbell: the whole "new" thing when I'm working with an array I've already created bothered me. But the textbook said...!!!
 
Karen Guffey
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

What this line does?It creates an array of length 6 and initializes it, and then passes as an argument to a method. Array itself holds elements 3, 1, 2, 6, 4, 2.
What do you want to do later with them within the method it is your choise.



So that statement could not be used to call a method called printArray?
 
Liutauras Vilda
Marshal
Posts: 4626
316
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Karen Guffey wrote:So that statement could not be used to call a method called printArray?
It can. But probably you don't want that, as you likely want to pass an array with elements entered by user (line 11).

Anyway, I removed your comments and changed misleading "list" variable name and some other irrelevant changes at the moment. This code should print array elements starting from the beginning (exactly as in your program):
Your exercise is to work out, how to amend lines 19 and 20 in order toget array elements printed backwards. How would you do that?
 
Karen Guffey
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One question before I can proceed: where do you create arrayToPrint?
 
Campbell Ritchie
Marshal
Posts: 55678
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Karen Guffey wrote:Campbell: Can I use a text editor with an IDE? We use Netbeans for class. If not, could I copy my program from Netbeans into the text editor for indentation? . . .!!!
Yes. But NetBeans should be able to sort out the indentation automatically. On Eclipse you can use ctrl‑A…ctrl‑I and it will indent the whole class. There must be a similar option on NetBeans.
 
Campbell Ritchie
Marshal
Posts: 55678
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Karen Guffey wrote:One question before I can proceed: where do you create arrayToPrint?
arrayTpPrint is the name of the parameter. You pass an array which can be any sort of array of the right type and the method will accept it.
 
Campbell Ritchie
Marshal
Posts: 55678
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Karen Guffey wrote:. . .
Back to Campbell: the whole "new" thing when I'm working with an array I've already created bothered me. But the textbook said...!!!
Do you play tennis?

It ought to have bothered you. I have already told you. You have an array with ten elements and then when you call the method you create another array inside the method call and the method uses that. The array you are creating is [9 8 7] and it printed out 9 8 7
 
Karen Guffey
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So the numbers you read in in the for loop populate arrayToPrint?
 
Karen Guffey
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I DID IT!!!



Thank you very much. This actually is a lot to wrap my head around. Specifically, the parameters thing, which my brain had a hard time computing before we got to arrays, is something I'm going to have to figure out how to file into the Java section of my brain. But you've gotten me over the hurdles, so I think I can proceed with what I have now & play around with until I really have a handle on it.

Thanks again!

BTW, Campbell, I did find a tool on Netbeans, but the complaint seems to be that it will only fix a section at a time. I'll play around with it, however, before I post anything here. (And if you guys are right, it might open my eyes & keep me from having to post!)

Hate tennis, so I won't post to Liutaurus in this post again!
 
Liutauras Vilda
Marshal
Posts: 4626
316
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
arrayToPrint is the parameter variable name, which you're going to use within your method locally.

1. Go through the tutorial about methods (<- link to read).
2. Go through the tutorial and remind yourself about the loops too (<- link to read).
3. Go through the tutorial about arrays (<- link too).
 
Campbell Ritchie
Marshal
Posts: 55678
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. They populate list, which is a bad name for an array, as Liutauras has already told you. They ought to populate arrayToPrint but you are creating a different array to pass as arrayToPrint in line 17. Look at Liutauras' version of the program which shows what you should have written.

Only Liutauras' version doesn't actually print backwards. He has left that part for you to work out.
Search my posts for for loops and their standard form. There are reasons for the standard form (search for that too) and if you are lucky and search my posts hard enough and get the right keywords, you will find something greatly to your advantage. But I cannot remember which keywords will actually do that.
 
Campbell Ritchie
Marshal
Posts: 55678
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Never use literal numbers in the headers of for loops to traverse arrays. Except the three numbers which you are permitted: 0, 1, −1. Always use myArray.length. So the standard form for a for loop to traverse an array backwards is
for (int i = something; i >= 0; i--) ...
Now you need to replace something with something else. And note the middle part does not require &&.

That is the standard form. You will occasionally need to traverse part of an array, or handle pairs of elements, in which case you need to change the standard form. But always start from the standard form.
 
Liutauras Vilda
Marshal
Posts: 4626
316
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another way to accomplish can be even without changing the standard form of the "for" loop.
Try to work out, what should be instead of the * on line 2
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!