• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

How to create an array out of existing array - but only nth element ?

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I need help with copying array, but only n-th element.

Let's say my oldarray is: a, b, c, d, e, f, g, h, i, j ...

I would like to copy only let's say second or third element to an brand new array. The results should be than:
b, d, f, h, j, ...for every second element in the newarray1
c, f, i, ...for every third element in the newarray2

Could anyone help?
 
Marshal
Posts: 15638
264
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some things to think about:
1. Do you know how to iterate over an array with a loop?
2. Do you know that a for-loop index can be increased by any amount, so instead of increasing by 1 for every iteration, you can increase by N instead?
3. If you have 9 elements and you only picked out every 3rd element, how many elements would you end up picking out? Can you come up with a formula to generalize the calculation?
 
Marshal
Posts: 69494
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That sounds easy enough. Please tell us how you think you would do it. There is no need to post a complete description, but make sure it is a description, not code.
 
Ivan Melicherc
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
1. yes
2.yes
3. the actual number of elements is unknown... and has to go through the whole old_array

I tried several things, but it did not work for me. What would be the easiest working solution? Thanks.
 
Campbell Ritchie
Marshal
Posts: 69494
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:. . . If you have 9 elements and you only picked out every 3rd element, how many elements would you end up picking out? . . .

Let's change that slightly:-

. . . If you have 10 elements and you only picked out every 3rd element, how many elements would you end up picking out? . . .

 
Ivan Melicherc
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Idea how to do this:
1. my array is long it is stored in an array - old_array
2. create a new empty array
3. Make a loop, as I don't know the length of the array, maybe it is better to use while loop? until is is empty?
4. Read only -nth element
5. Write nth element to the newarray1, newarray2

I have the basic idea, but it just didn't work for me.  
 
Ivan Melicherc
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Junilu Lacar wrote:. . . If you have 9 elements and you only picked out every 3rd element, how many elements would you end up picking out? . . .

Let's change that slightly:-

. . . If you have 10 elements and you only picked out every 3rd element, how many elements would you end up picking out? . . .



I know, I understand that it is still 3.
 
Junilu Lacar
Marshal
Posts: 15638
264
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ivan Melicherc wrote:
3. the actual number of elements is unknown... and has to go through the whole old_array


An array in Java is fixed length. It has a property, length, that will tell you how many elements it has so it's quite easy to determine how many you'll get if you pick out every Nth element.

I tried several things, but it did not work for me.


"It doesn't work is useless" - please TellTheDetails (←click that, it's a link)
 
Campbell Ritchie
Marshal
Posts: 69494
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another question: whatever the size of step is, are you always copying the fist element of the source array?

Ivan Melicherch wrote:. . . the actual number of elements is unknown... . . . .

So let's call the size of the source array x and the steps to follow y. So, Junilu's example comes to x = 9 and y = 3. Remember what the five arithmetic operators are in Java® (hint: +-*/%).  Try and work out a formula.
 
Ivan Melicherc
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, yes I know that length method exists. But still nothing worked for me, not even "finished" solutions that are online and also seemed to be too complicated for such an "easy" task. Therefore If anyone could help and post the most simplest solution. I would appriciate it. I know that I should learn, but I already spent a lot of time with this and just it does not work, my steps.
 
Campbell Ritchie
Marshal
Posts: 69494
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ivan Melicherc wrote:. . . length method exists.

Sorry fo misspelling your name earlier.

In the case of an array, length isn't a method but a final field

. . . "finished" solutions that are online and also seemed to be too complicated for such an "easy" task. . . . .

Unfotunately many online soutions to lots of things are less than ideal quality.
Right, if y is 3, how many elements are you going to write from input arrays lengths 9, 10, 11, and 12. Yes, you have to break the problem down into tiny little parts, until they appear to be idiotically simple. If they are that simple, then I can understand them
 
Ivan Melicherc
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whats going to be the next line to get it working?

for(int i = 0; i < oldarray.length; i+=4){
String[] newarray1;

how should the line look like if I want to copy every 4th element to the new array?
 
Campbell Ritchie
Marshal
Posts: 69494
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nothing. Look at the link I gave you. Don't try to work something out in code until you have worked it out on paper (or at least inside your head).

Or even look at the link I would have given you if I had remembered

[edit]Add 2nd line with link
 
Ivan Melicherc
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Nothing. Look at the link I gave you. Don't try to work something out in code until you have worked it out on paper (or at least inside your head).



That does not help me at all. And my thoughts how it should look like are written above in steps.
 
Campbell Ritchie
Marshal
Posts: 69494
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I forgot the link and added it to my last post.
If you don't work the problem out on paper, you have no hope of getting it working in code. Except possibly by guessing. Work out the formula without writing any code.
 
Ivan Melicherc
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Sorry, I forgot the link and added it to my last post.
If you don't work the problem out on paper, you have no hope of getting it working in code. Except possibly by guessing. Work out the formula without writing any code.



I already wrote the steps above. In 5 steps and i need to finish it, but it doesn't work for me. And you are sending me links not to code? That does not help at all...
 
Campbell Ritchie
Marshal
Posts: 69494
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your previous steps are vague and too large. You could probably get them working if you knew what size step to take and what size destination array to create. But you will have to subdivide some of those steps into smaller stages.
 
Ivan Melicherc
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Your previous steps are vague and too large. You could probably get them working if you knew what size step to take and what size destination array to create. But you will have to subdivide some of those steps into smaller stages.



Vague and large?

So the code I posted does count the length of the array... has a variable that represents the position I need in a for loop so it covers every nth-element. Now I need to copy it to a new array in that loop and that is still not understandable? How do you want to subdivide that task?

It's nice that you want to help, but I am asking only if I already spend a lot of time with it and it doesn't work. Words like don't code really do not help to finish it for me. I thought that I am asking someone who is willing to help to send a line or two of code, so that I don't have to spend another hours and hours of trying - and at the end it could be only a bracket for example.
 
Saloon Keeper
Posts: 7179
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Junilu Lacar
Marshal
Posts: 15638
264
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This site is Not A Code Mill so you are not going to get a finished solution from anyone here. That's your job, not ours.

We're here to help by giving tips and advice that we think will help you come up with a solution by yourself.
 
Campbell Ritchie
Marshal
Posts: 69494
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ivan Melicherc wrote:. . . So the code I posted does count the length of the array... . . . covers every nth-element. . . .

Yes,, that bit does, but you haven't worked out how to get the 2nd, 4th, 6th, etc. elements. Nor have you shown how to get the size of the destination array.

It's nice that you want to help, but I am asking only if I already spend a lot of time with it and it doesn't work. . . .

I am going to be away for at least the next hour. Use that hour for something completely separate from this problem. Fixating on what doesn't work will do nothing but annoy you.

send a line or two of code

And earn you a 0 for your assignment when you are accused of plagiarism? That's the last thing you want.

. . . it could be only a bracket for example.

It isn't that simple. It isn't very complicated, however.

What's more, I have realised that the difficulty of copying from array to array and the difficulty of sizing the new array are inversely proportional. If one is a bit harder, the other will be easier and vice versa.
 
Campbell Ritchie
Marshal
Posts: 69494
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I would do it slightly differently from your pseudocode, Carey.
 
Ivan Melicherc
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:This site is Not A Code Mill so you are not going to get a finished solution from anyone here. That's your job, not ours.

We're here to help by giving tips and advice that we think will help you come up with a solution by yourself.



That again does not help me. I came with several solutions that do not work for me. So it must be a stupid mistake - yes mine, but You are still not willing to help, because I should come up with the solution, but I did and it did not work, several times. That's why I am here and asking for help, asking how it should like the easiest way. I thought that I am in community, that can help with the code not in a phylosophic forum, where you only advice learn, it's not our job, read how NOT TO CODE. Such phylosophic non-sense does really not help at all.
 
Junilu Lacar
Marshal
Posts: 15638
264
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, this is a community but like most communities, it has rules and standards of conduct. The rules here are very clear: we don't give out solutions and we don't normally share solutions we would write until you've shown us one that you've done yourself. If you're looking for someone to write code for you, you can find that kind of help on other sites, not here.  
 
Ivan Melicherc
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:Yes, this is a community but like most communities, it has rules and standards of conduct. The rules here are very clear: we don't give out solutions and we don't normally share solutions we would write until you've shown us one that you've done yourself. If you're looking for someone to write code for you, you can find that kind of help on other sites, not here.  



So If I ran out of options how to continue, how to fix my own stuff I should leave this forum, according to you, because I can not figure it out myself. Great. Another great help.
 
Junilu Lacar
Marshal
Posts: 15638
264
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You've been given a number of options in previous replies. I haven't seen any effort on your part to try them and show what you tried. All you've done is complain that other people aren't doing your work for you. Maybe it is time to move on.
 
Ivan Melicherc
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:You've been given a number of options in previous replies. I haven't seen any effort on your part to try them and show what you tried. All you've done is complain that other people aren't doing your work for you. Maybe it is time to move on.



You mean those not to code? I spent already couple of hours with trying to fix it, find the issue, so sorry that you do not think that that is actually enough. And Yes I complained because phylosophic nonsense does not help at all...

Imagine that I have a cure for every disease, but I am not giving it to anyone, because in a phylosophical way natural selection is the only option how the mankind can survive in the future and not to have a faulty DNA. That does really help ill people right? (That's just a phylosophical way of thinking, you showed. - I know how to cure the illness, but I am not going to that, because it is not right, but I will show it to you after your friend dies.) Sometimes, it's just better to cross the border and even do something that you might think is not right, but in reality it is.
 
Junilu Lacar
Marshal
Posts: 15638
264
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But you have the cure... you're just not looking for it.

Again, what else have you tried? Post some code.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey gave you good solution earlier, work it
 
Climb the rope! CLIMB THE ROPE! You too tiny ad:
ScroogeXHTML 9.0 - RTF to HTML5 and XHTML converter
https://coderanch.com/t/731949/ScroogeXHTML-RTF-HTML-XHTML-converter
    Bookmark Topic Watch Topic
  • New Topic