Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have an array named numbers that I want to populate with a for loop:
int[] numbers;
for ( int i = 0; i <=10; i++)
{
// want to populate the array with a sequence of 0-10
}
How can I populate the 11 values generated from the above for loop into my array?
If you are using Java 7 or lower, do this:
int[] numbers = new int[11];
for ( int i = 0; i <=10; i++)
{
numbers[i] = i;
}
For Java 8, there is a more concise way to do this:
int[] numbers = IntStream.rangeClosed(0, 10).toArray()
First you need to define what numbers is, you have only declared it.
int[] numbers = new int[11];
Then insert the values you want.
for ( int i = 0; i <=10; i++)
{
numbers[i] = i;
}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I need to show the numbers 1-10 in a random order. An example outpue while executing first time would be: 5,4,8,7,9,1,2,3. An example while executing second time would be: 7,6,5,1,2,3,4,9,8
Will the following code print all date between ranges in random?
Random r = new Random();
int i1 = r.nextInt(80 - 65) + 65;
You can use the Collections.shuffle() method. (more info on this SO question)
In your example:
List<int> numbers = new ArrayList<int>();
int min = 65;
int max = 85;
for (int i = min; i <= max; i++) {
numbers.add(i);
}
Collections.shuffle(numbers);
Using shuffle works too. This is how you would manually do it.
Random r = new Random();
List<Integer> list = new ArrayList<Integer>();
for (int i=min ; i<= max ; i++){
list.add(i); // adding your data
}
List<Integer> list2 = new ArrayList<Integer>().addAll(list);
//you don't need to use
//list2 if you are ok with losing list.
//As here list2 is being emptied ..
while(list2.size() > 0){
int randomIndex = r.nextInt(0, list2.size);
System.out.println(list.get(randomIndex));
list2.remove(randomIndex);
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I want to transform a number into array.
For example...
num = 7
to
list = [1,2,3,4,5,6,7]
How do I do that?
Try this :
int []list = new int[num];
for (int i = 0; i < list.length; i++) {
list[i] = i + 1;
}
Try this:
Integer[] ints = new Integer[x];
for (int i = 0; i < ints.length; i++) {
ints[i] = i + 1;
}
I'm assuming you're want to create an array from 1 to the number.
Code:
for(int i = 0; i < num; i++) {
list[i] = i+1;
}
In java you have to declare the variables first.
This means that variables are strongly typed.
You cannot convert a variable into an array.
However you can create a new variable that is an array.
int num = 7;
int[] arr = {num}; // arr is an array containing num
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have to put into an integer for matches in the numbers of an array and another array, which do not have the same position in the array.
For example: I have these two Arrays of numbers:
4578
7539
It means that it have 1 number in the same position (5), and the number 7 is in the first array but not in the same position, so this case must increment 1 in my integer.
If it is in the same position like the number 5, I did this:
int introducido = Integer.parseInt(numero.getText());
for (int i = 0; i < String.valueOf(introducido).length(); i++) {
int entero = Integer.parseInt("" + numero.getText().charAt(i));
String temp = Integer.toString(numAleatorio);
int intarrNumeros = Integer.parseInt("" + temp.charAt(i));
if (intarrNumeros == entero) {
fijas++;
}
But I don't know how to do if is not in the same position.
UPD
Working for non-unique symbols in input strings
Try this code
pattern = "4578 ";
String toFind = "7539";
int samePosition = 0;
int notSamePosition = 0;
for (int i = 0; i < toFind.length(); ++i) {
char digit = toFind.charAt(i);
if (pattern.contains(String.valueOf(digit))) {
if (pattern.charAt(i) == digit) {
++samePosition;
} else {
++notSamePosition;
}
}
}
You can simply change the argument in the if statement to not equals.
if (intarrNumeros != entero)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
public int[] factors(int n) {
int count[] = new int[n];
for (int i = 1; i <= count.length; i++) {
count[i] = (count.length % i);
}
return count;
}
}
Anyone have an idea how to list all the factors?
set int i = 0; as array starts from 0
Factor can only be considered if value % i==0 so add if condition
You need one more variable to increment array Index as you can't use i for that.So add one more variable to increment array index to add factor.
Think more on your code, debug especially if possible,check out the values ,add print statements if needed to see what's going on and that's it you will definitely win the task.
Other than that if you can use SOF you can use Google as well :)
You should use a List instead of array. Since you can't initialize the array without knowing the size.
Then you can try something like following
public List<Integer> factors(int n) {
List<Integer> list = new ArrayList<>(); // initialize the list
for (int i = 1; i <= n; i++) {
if (n % i == 0) { // decide the factor.
list.add(i); // now i is a factor, needs to add to list
}
}
return list; // return list contains factors.
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
int arr[] = new int[10];
int size=0;
while(???)
{
i++;
}
System.out.println(size); // Should print 10
How can I loop over the array without using arr.length or other library functions?
int arr[] = new int[100];
int sum = 0;
int i = 0;
while (true) {
try {
sum += arr[i];
} catch (ArrayIndexOutOfBoundsException e) {
break;
}
i++;
}
System.out.println("Array is of size " + i);
I'm assuming array is of ints, but the idea is the same.
Under the hood you'll get a .length (unless you use a strange compiler), but since the question's kinda weird...
int[] array = new int[100];
int size = 0;
for(int i : array){
++size;
}
System.out.println("Size: " + size);
I still don't understand the point