loop through variables java android studio
up vote
0
down vote
favorite
I am a beginner in java and im having a bit of trouble looping through some variables;
I wanted to loop through the four variables and see if there were any repeated variables, then saving the non repeated in a second array or make a count, for example.
ValueA = "fred"
ValueB = "apple"
ValueC = "fred"
ValueD = "water"
Output count = 3
or array="fred","apple","water"
String arr = new String[4];
arr[0]= valueA;
arr[1]= valueB;
arr[2]= valueC;
arr[3]= valueD;
java
add a comment |
up vote
0
down vote
favorite
I am a beginner in java and im having a bit of trouble looping through some variables;
I wanted to loop through the four variables and see if there were any repeated variables, then saving the non repeated in a second array or make a count, for example.
ValueA = "fred"
ValueB = "apple"
ValueC = "fred"
ValueD = "water"
Output count = 3
or array="fred","apple","water"
String arr = new String[4];
arr[0]= valueA;
arr[1]= valueB;
arr[2]= valueC;
arr[3]= valueD;
java
3
Store them in a Set and duplicates would disappear.
– Pushpesh Kumar Rajwanshi
yesterday
What have you tried thus far? There are multiple ways to do this. Create a Set which doesn't allow duplicated; use a Java 8 stream with.distinct()
to remove duplicated items; etc.
– Kevin Cruijssen
yesterday
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am a beginner in java and im having a bit of trouble looping through some variables;
I wanted to loop through the four variables and see if there were any repeated variables, then saving the non repeated in a second array or make a count, for example.
ValueA = "fred"
ValueB = "apple"
ValueC = "fred"
ValueD = "water"
Output count = 3
or array="fred","apple","water"
String arr = new String[4];
arr[0]= valueA;
arr[1]= valueB;
arr[2]= valueC;
arr[3]= valueD;
java
I am a beginner in java and im having a bit of trouble looping through some variables;
I wanted to loop through the four variables and see if there were any repeated variables, then saving the non repeated in a second array or make a count, for example.
ValueA = "fred"
ValueB = "apple"
ValueC = "fred"
ValueD = "water"
Output count = 3
or array="fred","apple","water"
String arr = new String[4];
arr[0]= valueA;
arr[1]= valueB;
arr[2]= valueC;
arr[3]= valueD;
java
java
asked yesterday
Frederico Margarido
174
174
3
Store them in a Set and duplicates would disappear.
– Pushpesh Kumar Rajwanshi
yesterday
What have you tried thus far? There are multiple ways to do this. Create a Set which doesn't allow duplicated; use a Java 8 stream with.distinct()
to remove duplicated items; etc.
– Kevin Cruijssen
yesterday
add a comment |
3
Store them in a Set and duplicates would disappear.
– Pushpesh Kumar Rajwanshi
yesterday
What have you tried thus far? There are multiple ways to do this. Create a Set which doesn't allow duplicated; use a Java 8 stream with.distinct()
to remove duplicated items; etc.
– Kevin Cruijssen
yesterday
3
3
Store them in a Set and duplicates would disappear.
– Pushpesh Kumar Rajwanshi
yesterday
Store them in a Set and duplicates would disappear.
– Pushpesh Kumar Rajwanshi
yesterday
What have you tried thus far? There are multiple ways to do this. Create a Set which doesn't allow duplicated; use a Java 8 stream with
.distinct()
to remove duplicated items; etc.– Kevin Cruijssen
yesterday
What have you tried thus far? There are multiple ways to do this. Create a Set which doesn't allow duplicated; use a Java 8 stream with
.distinct()
to remove duplicated items; etc.– Kevin Cruijssen
yesterday
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
accepted
There are a lot of ways in doing this,
You can store the variables in a Set and get back an Array.
String withoutDuplicates = new HashSet<>(Arrays.asList(array)).toArray(new String);
Loop through first array -> have another empty list -> check before inserting into the second empty list. (You can try this since you are beginner, List already has contains() for you)
You can use Stream.filter().distinct() from Java 8
Arrays.stream(array).distinct().toArray(String::new);
I personally prefer the third one! :)
1
Just to expand, this assumes that anequals
method has been correctly implemented for custom objects.
– Moritz Sauter
yesterday
add a comment |
up vote
0
down vote
This should work:
String otherArr = new String[4];
int counter = 0;
for(int i = 0; i< arr.size(); i++)
for(int j = i+1; j< arr.size(); j++)
if( arr[i].equals(arr[j]))
//two similar variables
otherArr[counter++] = arr[i];
If you have more questions I can send you some decent guides.
New contributor
Did you read what the OP wants?
– forpas
yesterday
Does your code compile?
– forpas
yesterday
it's missing ';' at the end of otherArr[counter++] = arr[i]
– Ângelo D
yesterday
Should work now
– Ângelo D
yesterday
Should? Try it now.
– forpas
yesterday
|
show 2 more comments
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
There are a lot of ways in doing this,
You can store the variables in a Set and get back an Array.
String withoutDuplicates = new HashSet<>(Arrays.asList(array)).toArray(new String);
Loop through first array -> have another empty list -> check before inserting into the second empty list. (You can try this since you are beginner, List already has contains() for you)
You can use Stream.filter().distinct() from Java 8
Arrays.stream(array).distinct().toArray(String::new);
I personally prefer the third one! :)
1
Just to expand, this assumes that anequals
method has been correctly implemented for custom objects.
– Moritz Sauter
yesterday
add a comment |
up vote
0
down vote
accepted
There are a lot of ways in doing this,
You can store the variables in a Set and get back an Array.
String withoutDuplicates = new HashSet<>(Arrays.asList(array)).toArray(new String);
Loop through first array -> have another empty list -> check before inserting into the second empty list. (You can try this since you are beginner, List already has contains() for you)
You can use Stream.filter().distinct() from Java 8
Arrays.stream(array).distinct().toArray(String::new);
I personally prefer the third one! :)
1
Just to expand, this assumes that anequals
method has been correctly implemented for custom objects.
– Moritz Sauter
yesterday
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
There are a lot of ways in doing this,
You can store the variables in a Set and get back an Array.
String withoutDuplicates = new HashSet<>(Arrays.asList(array)).toArray(new String);
Loop through first array -> have another empty list -> check before inserting into the second empty list. (You can try this since you are beginner, List already has contains() for you)
You can use Stream.filter().distinct() from Java 8
Arrays.stream(array).distinct().toArray(String::new);
I personally prefer the third one! :)
There are a lot of ways in doing this,
You can store the variables in a Set and get back an Array.
String withoutDuplicates = new HashSet<>(Arrays.asList(array)).toArray(new String);
Loop through first array -> have another empty list -> check before inserting into the second empty list. (You can try this since you are beginner, List already has contains() for you)
You can use Stream.filter().distinct() from Java 8
Arrays.stream(array).distinct().toArray(String::new);
I personally prefer the third one! :)
answered yesterday
Mohamed Anees A
451413
451413
1
Just to expand, this assumes that anequals
method has been correctly implemented for custom objects.
– Moritz Sauter
yesterday
add a comment |
1
Just to expand, this assumes that anequals
method has been correctly implemented for custom objects.
– Moritz Sauter
yesterday
1
1
Just to expand, this assumes that an
equals
method has been correctly implemented for custom objects.– Moritz Sauter
yesterday
Just to expand, this assumes that an
equals
method has been correctly implemented for custom objects.– Moritz Sauter
yesterday
add a comment |
up vote
0
down vote
This should work:
String otherArr = new String[4];
int counter = 0;
for(int i = 0; i< arr.size(); i++)
for(int j = i+1; j< arr.size(); j++)
if( arr[i].equals(arr[j]))
//two similar variables
otherArr[counter++] = arr[i];
If you have more questions I can send you some decent guides.
New contributor
Did you read what the OP wants?
– forpas
yesterday
Does your code compile?
– forpas
yesterday
it's missing ';' at the end of otherArr[counter++] = arr[i]
– Ângelo D
yesterday
Should work now
– Ângelo D
yesterday
Should? Try it now.
– forpas
yesterday
|
show 2 more comments
up vote
0
down vote
This should work:
String otherArr = new String[4];
int counter = 0;
for(int i = 0; i< arr.size(); i++)
for(int j = i+1; j< arr.size(); j++)
if( arr[i].equals(arr[j]))
//two similar variables
otherArr[counter++] = arr[i];
If you have more questions I can send you some decent guides.
New contributor
Did you read what the OP wants?
– forpas
yesterday
Does your code compile?
– forpas
yesterday
it's missing ';' at the end of otherArr[counter++] = arr[i]
– Ângelo D
yesterday
Should work now
– Ângelo D
yesterday
Should? Try it now.
– forpas
yesterday
|
show 2 more comments
up vote
0
down vote
up vote
0
down vote
This should work:
String otherArr = new String[4];
int counter = 0;
for(int i = 0; i< arr.size(); i++)
for(int j = i+1; j< arr.size(); j++)
if( arr[i].equals(arr[j]))
//two similar variables
otherArr[counter++] = arr[i];
If you have more questions I can send you some decent guides.
New contributor
This should work:
String otherArr = new String[4];
int counter = 0;
for(int i = 0; i< arr.size(); i++)
for(int j = i+1; j< arr.size(); j++)
if( arr[i].equals(arr[j]))
//two similar variables
otherArr[counter++] = arr[i];
If you have more questions I can send you some decent guides.
New contributor
edited yesterday
New contributor
answered yesterday
Ângelo D
196
196
New contributor
New contributor
Did you read what the OP wants?
– forpas
yesterday
Does your code compile?
– forpas
yesterday
it's missing ';' at the end of otherArr[counter++] = arr[i]
– Ângelo D
yesterday
Should work now
– Ângelo D
yesterday
Should? Try it now.
– forpas
yesterday
|
show 2 more comments
Did you read what the OP wants?
– forpas
yesterday
Does your code compile?
– forpas
yesterday
it's missing ';' at the end of otherArr[counter++] = arr[i]
– Ângelo D
yesterday
Should work now
– Ângelo D
yesterday
Should? Try it now.
– forpas
yesterday
Did you read what the OP wants?
– forpas
yesterday
Did you read what the OP wants?
– forpas
yesterday
Does your code compile?
– forpas
yesterday
Does your code compile?
– forpas
yesterday
it's missing ';' at the end of otherArr[counter++] = arr[i]
– Ângelo D
yesterday
it's missing ';' at the end of otherArr[counter++] = arr[i]
– Ângelo D
yesterday
Should work now
– Ângelo D
yesterday
Should work now
– Ângelo D
yesterday
Should? Try it now.
– forpas
yesterday
Should? Try it now.
– forpas
yesterday
|
show 2 more comments
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53224478%2floop-through-variables-java-android-studio%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
3
Store them in a Set and duplicates would disappear.
– Pushpesh Kumar Rajwanshi
yesterday
What have you tried thus far? There are multiple ways to do this. Create a Set which doesn't allow duplicated; use a Java 8 stream with
.distinct()
to remove duplicated items; etc.– Kevin Cruijssen
yesterday