Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have created 2 ArrayLists of the same size (5) and i want to print out their contents side by side in columns.
For example:
list 1 = [1 , 2 , 3 , 4 , 5]
list 2 = [5 , 4 , 3 , 2 , 1]
I want to print it out to the console like this:
1 5
2 4
3 3
4 2
5 1
I have tried using a for loop within another for loop but I think I may be overthinking it.
Since, you need the lists to print side by side nested loops are not required.
for (int i = 0 ; i < list1.size(); i++) {
System.out.printf("%d\t%d\n", list1.get(i), list2.get(i));
}
Notice, the loop assumes that the two lists are of the same size. So, we need only one loop counter.
Output :
1 5
2 4
3 3
4 2
5 1
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 7 years ago.
Improve this question
I have a question: Can some one give me an example of searching the binary table to find the change from 1 to 0? For example, one line have
0 0 0 1 1 0 0 0 0 1 1 1 0 0
and it should give me 2 changes. I want to search only one line.
for(int i=1; i< binarytableline.length; i++){
if (binarytableline[i - 1] == 1 && line[i] == 0) changes++;
}
Remember that the indexes start at zero:
(Java docs)
Essentially, whats happening in the code above is that we are looping through the line. Now, i is the index. Lets start at the beginning of the loop.
i=1
So, binarytableline[1-1] = the first index of the binary table line, or the very first number. Now, we are seeing if it equals 1, and the second index, which is i, equals 0. To check, we do the following:
binarytableline[i - 1] == 1 && line[i] == 0
This would mean that there is a change in binary digits from one digit to the next, and in our example that is the 0th index to the first. Now, we would iterate our variable, changes by 1 by doing changes++. Again, this is in a for loop, meaning we will loop through all the elements like this. The amount of times it has changed will be recorded in int changes.
Let me know if this helped,
Ruchir
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 7 years ago.
Improve this question
Can you help me for my assignment. I don't know how to execute this kind of outputs?
1
2 3
4 5 6
7 8 9 10
and
1 2 3 4
5 6 7
8 9
10
and lastly
1 2 3 4 5 4 3 2 1
1 2 3 4 4 3 2 1
1 2 3 3 2 1
1 2 * 2 1
1 * * * 1
1 2 * 2 1
1 2 3 3 2 1
1 2 3 4 4 3 2 1
1 2 3 4 5 4 3 2 1
Thank you!
- 1st year programming student.
i think you could do something like
"var i = 1"
"var spaces = 1"
"var counter = 1"
"length = 1"
while(i < 11)
{
for(spaces <= counter)
{
print i
i++
spaces ++
}
print (pass line)
spaces = 0
counter ++
}
this way you will get to print the var "i" until it gets to 11
and with the counter and the spaces var you will pass a line at 1,2,3.... numbers respectivily.
but since its your homework and you didnt specified in which language do you want the code i will let that to you. also dont ask us to do your code in the future.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I have a loop that checks the biggest number so far and adds one to it; but for example 1 2 3 4 5 I press button 6 is created, what if the list was 1 2 3 5 6 and I want it to print 4 so I can get a correct number line then proceed to increase it by one?
EDIT: I should mention that my list has a possiblity not being in order so 2 1 3 5
You should check incrementally that the numbers are good:
So say you have an array of int called numbers[]
you could do:
int placehodler = numbers[0] + 1;
for (int i = 1; i < numbers.length(); i++){
if (placeholder != numbers[i]) // checks if the next number is equal to the previous + 1
break; // breaks out of the loop if it isn't
placeholder++; // increment placeholder by 1
}
System.out.println(placeholder); // prints placeholder
So if the placeholder breaks out of the loop early, it is your 1,2,3,5,6 situation and it would have 4 (succeeds on the loop where placeholder = 3, fails when placeholder = 4) in the 1,2,3,4,5 situation, it would succeed on all and end the loop with a value of 6.
First, I would find the count of your current list. If your list is [1 2 3 5] the count is 4.
Then, I would create a new list that is a range of numbers from 1 to the count => [1 2 3 4]
comparing the two lists will let you see if any numbers are missing, if no numbers are missing then you can add 1 to the current count and append that to the end of the list.
sorting the list before doing any of this will take care of the out of order issue mentioned in the other comment.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I would like to split my strings in JAVA based on a regular interval, not on regex. This is what I have to split:
1 x3.1.105.41 1 -10
2 x4.1.105.41 0 -10
3 x12.1.105.41 0 -10
4 y3.1.105.41.19 1 0
5 y4.1.105.41.21 0 0
6 y1.1.105.41.23 0 0
7 y12.1.105.41.25 0 0
I would like to seperate each column. Currently, I use the strLine.spli function
Any help would be great!
You can use substring:
String myLine = "1 x3.1.105.41 1 -10";
String column1 = myLine.substring(0, 2).trim();
String column2 = myLine.substring(2, 20).trim();
...
Or just split the lines:
String myLine = "1 x3.1.105.41 1 -10";
String[] columns = myLine.trim().split("\\s+");
which gives you in columns[0] your first value, in `columns[1]ยด your second and so on.
The second solution looks smarter to me.
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 9 years ago.
Improve this question
I have the following list of lists List<List<Integer>>
1 2
3
4 5 6
7
8
9
10
I have to group randomly this list of lists as 3 subsets with 4 elements each (nevermind that in the last subset i'll have less than 4 elements), and in every subset i cant have elements from the same row ( i cant have 3 4 5 7 for example). What should be the basic algorithm????
What should be the basic algorithm????
Pluck four random elements from each of the first four1 randomly chosen non-empty lists in the list of lists. Remove the chosen items, and repeat for the next subset. Remove empty lists as you go.
Next, you need to note that not all of your inputs are solvable:
1
2
3
4
5 6 7 8 9
In this case, we choose randomly
1*
2*
3*
4
5 6 7 8 9*
-> 7 1 2 3 (chosen randomly)
[]
[]
[]
4*
5 6 8 9*
-> 5 4 . . <- uh, impossible to choose four items from different lists even though non-empty lists remain. You can detect this during iteration.
1: Or, choose randomly from all of the lists if less than four non-empty lists remain.
The easiest answer is to perform a random selection, check whether your "no two from the same" rule is violated. If it is, then throw away and try again.
Sort the lists by number of elements.
Pick 4 elements, one from each of the 4 lists with the most elements.
Resort.
Repeat from 2.
A heap could help here.
Why do we need to pick from the lists with the most elements?
Consider this case:
1 2
3 4
5 6
7
8
If we pick both 7 and 8, we'll be left with something like:
1 2
3
5
From here there's no possible way to pick 4 elements from different lists.
However, if we pick elements from the lists with the most elements, we'd be left with something like:
1
3
5
8
From here we can just pick one element from each list.