0

(java beginner here) I am following this Random shuffling of an array but what i want to do is instead of randomly changing elements in an array then using it in my method. I want to make sure the order of the changing list is going from [1]..[2]..[3]..... (basically increasing) My ultimate goal is to have a table of numbers

int[] a = {1,2,3,4,5,6} then it in my method

static void shuffleArray(double[] solutionArray)
{
    Random rnd = new Random();
    for (int i = solutionArray.length - 1; i > 0; i++)
    {
      int index = rnd.nextInt(i + 1);
      int value = 0;
     // int charValue = value.digit(0);
      String next = String.valueOf( (double) (value + 1));
      System.out.println(next);
      // Simple swap
      double a = solutionArray[index];
      solutionArray[index] = solutionArray[i];
      solutionArray[i] = a;
    }

  }

public static void main(String[] args)
{
  double[] solutionArray = { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7};
  double[] solution = { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7};
  shuffleArray(solutionArray);
  shuffleArray(solution);

  for (int i = 0; i < 7; i++)
  {
      position.setspeed(solutionArray[0]);
  }
}

but when i set it or use it in a method i want it to go from 1 to 6 instead of randomly choosing an element or from the link shuffling the element.

Community
  • 1
  • 1
  • 1
    I don't understand, can you explain it a bit clearer? btw, your `for` loop in `shuffleArray()` never terminates. – Blub Nov 26 '13 at 10:47
  • apologies for misleading everyone, its supposed to be: for (int i = solutionArray.length - 1; i >= 0; i--) {} –  Nov 26 '13 at 21:57

2 Answers2

0

This line can't be right:

for (int i = solutionArray.length - 1; i > 0; i++)

If you are looping backwards, it should be:

for (int i = solutionArray.length - 1; i >= 0; i--)

If you are looping forwards, it should be:

for (int i = 0; i < solutionArray.length; i++)
NickJ
  • 9,380
  • 9
  • 51
  • 74
  • apologies for misleading everyone, its supposed to be: for (int i = solutionArray.length - 1; i >= 0; i--) {} –  Nov 26 '13 at 21:57
0

For having table of number

import java.util.Scanner;

public class ArrayTable {

public static void main(String[] args) {
     int a[]={1,2,3,4,5,6,7,8,9,10};

    int solution[]=new int[10];


    Scanner sc=new Scanner(System.in);
    System.out.println("Enter the number");
    int num=sc.nextInt();

    for(int i=0;i<a.length;i++)
    {
        solution[i]=a[i]*num;
    }
    for(int y:solution)
    {
        System.out.print(y+"\n");
    }
}
}
Swanand Keskar
  • 1,023
  • 1
  • 13
  • 27