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;









share|improve this question

















  • 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















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;









share|improve this question

















  • 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













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;









share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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













  • 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













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! :)






share|improve this answer
















  • 1




    Just to expand, this assumes that an equals method has been correctly implemented for custom objects.
    – Moritz Sauter
    yesterday


















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.






share|improve this answer










New contributor




Ângelo D is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

















  • 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










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















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






























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! :)






share|improve this answer
















  • 1




    Just to expand, this assumes that an equals method has been correctly implemented for custom objects.
    – Moritz Sauter
    yesterday















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! :)






share|improve this answer
















  • 1




    Just to expand, this assumes that an equals method has been correctly implemented for custom objects.
    – Moritz Sauter
    yesterday













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! :)






share|improve this answer












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! :)







share|improve this answer












share|improve this answer



share|improve this answer










answered yesterday









Mohamed Anees A

451413




451413







  • 1




    Just to expand, this assumes that an equals method has been correctly implemented for custom objects.
    – Moritz Sauter
    yesterday













  • 1




    Just to expand, this assumes that an equals 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













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.






share|improve this answer










New contributor




Ângelo D is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

















  • 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














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.






share|improve this answer










New contributor




Ângelo D is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

















  • 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












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.






share|improve this answer










New contributor




Ângelo D is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









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.







share|improve this answer










New contributor




Ângelo D is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this answer



share|improve this answer








edited yesterday





















New contributor




Ângelo D is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









answered yesterday









Ângelo D

196




196




New contributor




Ângelo D is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Ângelo D is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Ângelo D is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











  • 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










  • 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

















 

draft saved


draft discarded















































 


draft saved


draft discarded














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














































































Popular posts from this blog

Use pre created SQLite database for Android project in kotlin

Darth Vader #20

Ondo