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 6 of 6

Thread: Problems with replacing indexs in an array

  1. #1
    Junior Member
    Join Date
    Jun 2019
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problems with replacing indexs in an array

    Hi guys,im learning java and i have an assigment which says
    "Make an array with 20 int and replace the first index with the 10th and so on,"
    Ive wrtiing the code,but it somewhat works only when i use 8as my first index insted of 9
    so..um..whats wrong with it?





    package page48;
     
    import java.util.Arrays;
    import java.util.Random;
     
    public class Drill4 {
     
    	public static void main(String[] args) {
     
    		int[] array = new int[20];
    		Random rand = new Random();
     
    		int temp = 0;
     
    		for (int i = 0; i < array.length; i++) {
    			array[i] = rand.nextInt(20) + 1;
     
    		}
    		System.out.println(Arrays.toString(array));
    		for (int i = 8; i < array.length; i++) {
     
    			for (int j = 0; j < array.length; j++) {
    				temp = array[i];
    				array[i] = array[j];
    				array[j] = temp;
     
    			}
     
    		}
    		System.out.println(Arrays.toString(array));
     
    	}
    }

    The console::
    [16, 6, 9, 8, 4, 5, 8, 16, 15, 14, 18, 6, 12, 18, 19, 13, 7, 19, 13, 1]
    [18, 6, 12, 18, 19, 13, 7, 19, 13, 1, 16, 15, 16, 6, 9, 8, 4, 5, 8, 14]

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,199
    Thanks
    53
    Thanked 2,385 Times in 2,339 Posts

    Default Re: Problems with replacing indexs in an array

    it somewhat works only when i use 8as my first index insted of 9
    Can you post the different results that show what you are talking about? Add some comments to the output describing the problem and show the desired output.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jun 2019
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problems with replacing indexs in an array

    if i use 8 inside the second loop fo i
    [16, 6, 9, 8, 4, 5, 8, 16, 15, 14, 18, 6, 12, 18, 19, 13, 7, 19, 13, 1]
    [18, 6, 12, 18, 19, 13, 7, 19, 13, 1, 16, 15, 16, 6, 9, 8, 4, 5, 8, 14]
    it somewhat works,until it reaches the middle and than gets all wonky,
    on second try-
    [9, 1, 12, 5, 20, 17, 16, 12, 15, 9, 9, 14, 19, 1, 6, 2, 5, 4, 20, 5]
    [9, 14, 19, 1, 6, 2, 5, 4, 20, 5, 12, 15, 9, 1, 12, 5, 20, 17, 16, 9]
    Still...works a bit wonky but works halfway.
    If i use 10 for the index in the second loop,(which logicaly thinking sounds right to me)
    [2, 3, 2, 6, 20, 2, 19, 17, 20, 13, 3, 2, 10, 13, 15, 2, 17, 19, 6, 12]
    [10, 13, 15, 2, 17, 19, 6, 12, 13, 3, 2, 3, 2, 6, 20, 2, 19, 17, 20, 2]
    it starts the swap from the 13 number insted of the 10th

    The way i want it to be is
    [16, 6, 9, 8, 4, 5, 8, 16, 15, 14, 18, 6, 12, 18, 19, 13, 7, 19, 13, 1]
    [18, 6, 12, 18, 19, 13, 7, 19, 13, 1,16, 6, 9, 8, 4, 5, 8, 16, 15, 14]

    Edit**
    ive changed the second loop and got it working,but i still fail to see why it didnt work before,
    heres the new loop
    package page48;
     
    import java.util.Arrays;
    import java.util.Random;
     
    public class Drill4 {
     
    	public static void main(String[] args) {
     
    		int[] array = new int[20];
    		Random rand = new Random();
    		int j = 0;
    		int temp = 0;
     
    		for (int i = 0; i < array.length; i++) {
    			array[i] = rand.nextInt(20) + 1;
     
    		}
    		System.out.println(Arrays.toString(array));
    		for (int i = 10; i < array.length; i++) {
    			temp = array[i];
    			array[i] = array[j];
    			array[j] = temp;
    			j++;
     
    		}
    		System.out.println(Arrays.toString(array));
     
    	}
    }
    Last edited by mike512; June 21st, 2019 at 07:25 AM.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,199
    Thanks
    53
    Thanked 2,385 Times in 2,339 Posts

    Default Re: Problems with replacing indexs in an array

    What does the value of 8 represent? Why was that value chosen as a loop start point?

    replace the first index with the 10th and so on,
    I don't understand what those instructions say. Can you show an example on an array with numbers in order:
    Given this input: [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
    what is the desired output?

    Using random values for testing makes it hard to see if the code is making the desired result.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jun 2019
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problems with replacing indexs in an array

    If this are my "random" numbers,my first outpring will show them as they are

    [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]

    and than i need to swap locations,
    [10,11,12,13,14,15,16,17,18,19,1,2,3,4,5,6,7,8,9]
    Ive mannaged to do so,check my edit to my previous reply,but im stil lhaving trouble with replacing diffrent values inside the array,cant get my head around it

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,199
    Thanks
    53
    Thanked 2,385 Times in 2,339 Posts

    Default Re: Problems with replacing indexs in an array

    why it didnt work before
    Because the logic was wrong.
    There was no need for a nested loop.
    The program is simply swapping elements from index starting at 0 (in j) with those from index starting at 10 (in i)
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replacing characters in a an array
    By javaStooge in forum What's Wrong With My Code?
    Replies: 7
    Last Post: March 9th, 2014, 01:19 PM
  2. JComboBox and array problems
    By tristannvk in forum Collections and Generics
    Replies: 6
    Last Post: December 23rd, 2012, 09:39 AM
  3. [SOLVED] 2 dimentional boolean array problems!
    By masterT in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 14th, 2011, 11:03 PM
  4. Copying Array Problems.. Not what you think
    By xXRedneckXx in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 5th, 2011, 11:01 AM
  5. Array problems (printing strings)
    By James5955 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 12th, 2011, 12:21 AM