How to transform a number into array. Java [closed] - java

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

Related

Finding greatest difference between elements of two lists [closed]

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 2 years ago.
Improve this question
My code is:
int diff = 0;
for (int i = 0; i<listOne.size(); i++)
{
for (int j = 0; j<listTwo.size(); j++)
{
if (listOne.get(i)-listTwo.get(j)>diff)
diff = listOne.get(i)-listTwo.get(j);
if (listTwo.get(j)-listOne.get(i)>diff)
diff = listTwo.get(j)-listOne.get(i);
}
}
return diff;
The task is to find the greatest difference between any two numbers in two inputted lists (the difference must be between a number from list one and a number from list two).
I cannot tell what is wrong with my code.
You may be missing Math.abs executed on the diff. Difference is an absolute value, so difference between 5 to 7 and 7 to 5 is same - 2.
int diff = 0;
for (int i = 0; i<listOne.size(); i++) {
for (int j = 0; j<listTwo.size(); j++) {
int elementDiff = Math.abs(listOne.get(i)-listTwo.get(j));
if (elementDiff>diff) {
diff = elementDiff;
}
}
}
return diff;
So both below lines will produce same results:
int elementDiff = Math.abs(listOne.get(i)-listTwo.get(j));
int elementDiff = Math.abs(listTwo.get(i)-listOne.get(j));

How to print out possible string with alphabet set and length? [closed]

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 3 years ago.
Improve this question
I am using java
For example, the following situation:
First, the function is used as a print all possible strings.
char[] alphabetSet = "012abc".toCharArray();
int length =5;
Output:
0,1,2,a,b,c,01,02,0a,0b,0c,10,11,12,1a ..................... ccccc. stop in length = 5
Then, I want to add a loop stopper to fetch the specified string.
char[] alphabetSet = "012abc".toCharArray();
int length =5;
int loopStopper = 3;
Output:
a
Thank you
Use backtracking.
void print_all(char []ch,int maxLen){
for(int i=1;i<=maxLen;i++)
backTrack(ch,i,0,new char[i]);
}
void backTrack(char[] ch,int len,int k,char[] ans){
if(k==len){
System.out.print(new String(ans,0,len)+",");
return;
}
for(int i=0;i<ch.length;i++){
ans[k]=ch[i];
backTrack(ch,len,k+1,ans);
}
}
try this:
String alphabet = "012abc";// for example as your code "012abc"
char[] alphabetSet = alphabet.toCharArray();
int length = 5;
for (int i = 0; i < alphabetSet.length; i++) {
System.out.print(alphabetSet[i] + ",");
}
for (int j = 0; j <= length; j++) {
for (int i = 0; i < alphabetSet.length; i++) {
System.out.printf("%d%c,",j,alphabetSet[i]);
}
}

How can I get a random number between two ranges? [closed]

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);
}

Array processing in Java [closed]

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 6 years ago.
Improve this question
I have this String array in which is made up of string representing a student id, first name, last name, email,and grades. My question is: Is there away I can split each entry in this array and be able to calculate the average grade of student in this sample array entry. I would appreciate if anyone can offer some solution on how to achieve that in this array.
String[] students = {"1,John,Smith, John1010#fakemail.com ,20,88,79,59",
"2,Suzan,Erickson, Sue9999#fakemail.com ,19,91,72,85",
"3,Jack,Napoli, Jack789#fakemail.com,19,85,84,87",
"4,Erin,Black, Aaron888#fakemail.com,22,91,98,82"};
Here a piece of code that does what you want:
String[] students = ...
double[] averages = new double[students.length];
for (int i = 0; i < students.length; i++) {
String[] student = students[i].split(",");
int sum = 0;
for (int j = 4; j < student.length; j++) {
sum += Integer.parseInt(student[j]);
}
averages[i] = (double) sum / (student.length - 4);
}
The array averages will contain all the averages at the same position. Note that this code doesn't handle any wrong format and that it's assuming that all the remaining values are grades.
I guess writing code for you will not be so appropriate... However, I can suggest what needs to be done:
1. Split each string by using String class split(regex). You can simply split the string with comma.
(https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#split(java.lang.String))
2. Just add the grade and divide by the size of students array.
I am sorry if you were expecting someone would provide the code...but learning by doing!!
public static void main(String[] args) {
int grades = 0;
String[] students = { "1,John,Smith,John1010#fakemail.com,20,88,79,59",
"2,Suzan,Erickson,Sue9999#fakemail.com ,19,91,72,85", "3,Jack,Napoli,Jack789#fakemail.com,19,85,84,87",
"4,Erin,Black,Aaron888#fakemail.com,22,91,98,82" };
for (int i = 0; i < students.length; i++) {
String[] student = students[i].split(",");
for (int j = 4; j < student.length; j++) {
grades += Integer.parseInt(student[j]);
}
grades=0;System.out.println(student[2] + ": " +(double) grades / (student.length - 4));
}
}

Populate int array with for loop in Java [closed]

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;
}

Categories