You don't need to go on hikes necessarily to train for hiking either.
But IMO hikes are best.
Years ago I did a GC hike with a friend and 2 other guys he knew from work. The 2 guys were marathoners and appeared to be quite fit, but had very little hiking experience.
We did Tanner/Escalante/New Hance, doing a dry camp about 2/3 of the way down Tanner the first day in order to see a nice sunrise over the canyon the next day.
The 2 guys did no trail training, and that morning of the beautiful sunrise they were barely able to get out of their tent. Their legs had absolutely no conditioning for downhill, and they had to pop some Advil and wait about an hour before they could move without serious pain.
I don't do gyms and perhaps there's some exercise machine out there I haven't heard of, but I don't think there's any way to truly replicate hiking downhill. As for myself, although I was pretty trail fit at the time I still drove up to the Catskills a few days before the trip and did the nasty little section from Devil's Tombstone toward Devil's Acre 3 times in a row... it rises about 1500 feet in a mile.