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 Set intersection(Set set){
Set intersect = new Set(this.count + set.count);
for(int i=1; i<count; i++)
{
if(this.items[i] && set.items[i])
intersect.add(i);
return intersect;
}
To find the intersection of two sets you can use the function retainAll.
Set<Integer> s1;
Set<Integer> s2;
s1.retainAll(s2);
After this, s1 will contain the intersection.
You could also use this method for arrays if you don't mind converting them to a set or list first, e.g.:
int a[] = {1,2,3};
Set<Integer> mySet = new HashSet<Integer>(Arrays.asList(a));
Related
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 4 days ago.
Improve this question
How to write a loop that will delete those elements of the collection whose numbers form an arithmetic progression, by type if the number 246 deletes, but the second 245 does not, since it does not oblige the progression
import java.util.ArrayList;
public class n {
public static void main(String[] args) {
ArrayList nums = new ArrayList<>();
nums.add(24);
nums.add(223);
nums.add(2424);
nums.add(135);
nums.add(258);
nums.add(246);
for (int i = 0; i < nums.size();i++){
if
}
}
}
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 10 months ago.
Improve this question
I have a simple for loop
and I want to change it into a statement using filter
for (int lotto_num: lottos) {
if (lotto_num == 0) {
unknown_count++;
}
for (int win_num : win_nums) {
if (lotto_num == win_num) {
count ++;
}
}
}
is it possible? I dont't really know how to use filter and stream.
In order to use Java's Stram API you should firstly convert your int[] arrays win_nums and lottos to List<Integer>.
It can be done this way:
List<Integer> win_nums_list = IntStream.of(win_nums).boxed().collect(Collectors.toList());
List<Integer> lottos_list= IntStream.of(lottos).boxed().collect(Collectors.toList());
Now we can use Stream API to get the desired result.
int count = win_nums_list.stream().filter(win_num -> lottos_list.contains(win_num)).count();
int unknown_count = lottos_list.stream().filter(lotto -> lotto== 0).count();
with adding imports to the beginning of your code:
import java.util.*;
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 am trying to run some objects methods in this way:
String[] sequencer = {"seq1", "seq2", "seq3", "seq4", "seq5", "seq6", "seq7", "seq8"};
for(int i; i<9; i++) {
sequencer[i].setBackground(Color.red);
}
seq1, seq2..seq8 are jPanels. Any idea how to do this ? I hope you understand what I want to do.
Can you do this? Or are you looking for something else?
JPanel [] sequencer = new JPanel[]{seq1, seq2, seq3, seq4, seq, seq6, seq7, seq8};
for(int i; i<9; i++) {
sequencer [i].setBackground(Color.red);
}
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 have a list of Portfolio objects in java collection. This Portfolio object has a property called scoringRule. I want to calculate how much percentage of Portfolio objects are present in the list for each type of scoringRule. Actually, I have only 2 different types of scoring rules. The scoringRUle property can be either "single" or "double". How can I do this?
Class Portfolio{
private String name;
private String type;
private String scoringRule;
}
List<Portfolio> portfolios = new ArrayList<Portfolio>();
This is your solution.
double single = 0;
double doublee = 0;
for (Portfolio pobj : portfolios) {
if(pobj.scoringRule.equals("single"))
single++;
else
doublee++;
}
System.out.println("single Percentage : " +(single / list.size() )*100);
System.out.println("doublee Percentage : " + (doublee / list.size())*100);
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 9 years ago.
Improve this question
public Integer[] imgs = new Integer[]{
R.drawable.ic_launcher,R.drawable.ic_launcher
};
String s_img = "R.drawable.img_test";
i want
imgs[0] = s_img
imgs[0] == R.drawable.img_test
true !!
What shall I do?
Try this:
s_img = "img_test";//not "R.drawable.img_test" just the name of the image
if(getResources().getIdentifier(s_img, "drawable", getPackageName()) == imgs[0]){
return true;
}else{
return false;
}
You want an array of drawables, right?
Integer[] drawables = {R.drawable.image1, R.drawable.image2, ....};
And you can access values by:
drawables[0] == R.drawable.image1
drawables[1] == R.drawable.image2
And you loop them aswel.
for comparing Object do not use "=="
try
"R.drawable.img_test".equals(Integer.toString(imgs[0]));
Put them in an Enum instead of array.
tell me is that exactly what you need or not ,i think i git what you need .
public Integer[] imgs = new Integer[]{
R.drawable.ic_launcher,R.drawable.ic_launcher
};
String s_img;
s_img = Integer.toString(x);