Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Problem with arrays

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    24
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Problem with arrays

    Good evening everyone , I am curently having problem since i want to make a basic program which basically reverse all the numbers of a certain array into the same array , by that i mean i want to overwrite my array into a new one but with it's number changed. I am still a basic programmer so sorry for not understanding my problem thank you.
    public class Reverse {
    	public static void main(String[] args) {
    		int [] numbers = {1,2,3,4,5,6,7,8,9};
    		reverseInPlace(numbers);
     
     
    	}
     
    	public static void reverseInPlace(int[] a) {
    		a = new int[10];
    		for (int i = 0; i <= a.length / 2; i++) {
    			int temp = a[i];
    			a[i] = a[a.length - 1 - i];
    			a[a.length-1-i]=temp;
    		}
     
    	}
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problem with arrays

    What problems are you having with the program?

    The Arrays class's toString() method is useful for formatting an array for printing:
    System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2013
    Posts
    24
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Problem with arrays

    The problem is that i get no output , i know how to use arrays.ToString but the problem is i get no output.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problem with arrays

    What code do you expect to produce any output? I don't see any println() statements in the code.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Problem with arrays

    Probably because when you pass the array into the reverseInPlace() method, you immediate reset it to a list of 10 zeros (that's what the first line in your method does). So when you run the loop, you are operating on all 0s, so of course you won't get the output you want.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

Similar Threads

  1. Problem with arrays
    By Jad_Asmar in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 23rd, 2013, 12:58 PM
  2. Problem with Arrays in code !!!!
    By Cluelius in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 8th, 2012, 06:58 PM
  3. [SOLVED] Problem with Arrays
    By dashpilot in forum Object Oriented Programming
    Replies: 4
    Last Post: March 6th, 2012, 10:11 AM
  4. Problem with arrays
    By coorscollector in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 21st, 2011, 03:50 AM
  5. 2 D arrays problem
    By Pulse_Irl in forum Collections and Generics
    Replies: 2
    Last Post: March 5th, 2010, 04:49 PM