using objects names (like panels) from an array of strings [closed] - java

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

Related

How to rught a loop that delete elents of collection,which creat arifmetick progression [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 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
}
}
}

How to convert to "filter" from for loop [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 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.*;

For loop that outputs 0,-1,1,-2,2,-3,3 [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 5 years ago.
Improve this question
I am making a Java game and am trying to get blocks to spawn alternating back and forth on the x-axis using a for loop.
Just do something like this:
spawn(0);
for(int i = 1; i <= 3; ++i)
{
spawn(-i);
spawn( i);
}
Method createBlock(): //creates the blocks at desired x axis
createBlock(0); //initial block placed at 0
int howMany= input
for (i=0; howMany;i++){
createBlock(-i)
createBlock(i)
}
IntStream.iterate(0, i -> i >= 0 ? -i - 1 : -i). limit(100).forEach(i -> System.out.println(i));
System.out.print(0);
for (int i = 1; i<=3; i++) {
System.out.print(String.format(",%s,%s", -i, i));
}

Multiples of 20 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 done a coding to get the output 20,40,60,80 and 100.
Multiple.java:
public class Multiple {
static int n=20;
static int m;
public static void main(String[] args) {
for(int i=1;i<=5;i++){
m=n*i;
System.out.println(m);
}
}
}
Output:
20
40
60
80
100
I didn't have any issue in this coding.I got an output successfully.But loop running 5 times.Let me know is there any other way to get these output(20,40,60,80 and 100) in simplest way.
very interesting. if you know the output result (it's fixed), why don't you write them out directly:
System.out.println(20);
System.out.println(40);
System.out.println(60);
System.out.println(80);
System.out.println(100);`
oh i see, you think that's too long. Then probably you can use the below:
int a = 0;
while(a<100){ a += 20; System.out.println(a);}
Enjoy it!

How to get the intersection between two array using 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
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));

Categories