Finding the first unique integer in int[] - java

Given an integer array, I want to return the first unique element in the array. I used A List .contains() method to check if Integer array contains the element, the method is correct but not efficient(time complexity O(N**2)) since List.contain() loops entire list comparing each element.
List<Integer> list = new ArrayList<Integer>();
int num = 0;
for(int a: A){
if(list.contains((Integer)a)){
list.remove((Integer)a);
}else{
list.add(a);
}
}
num = !list.isEmpty()? (int) set.get(0): 0;
return list.size()<1?-1:num;
}
//example input/output
int[] a = {1,2,6,1,6}
//I get the correct answer 2
Done my research and found that HashSet has Contains which is more efficient
Problem is Once i use a HashSet(I also tried Set) I dont get same result. The function should return the first unique element in the int[]
import java.util.*;
HashSet<Integer> set = new HashSet<Integer>();
int num = 0;
for(int a: A){
if(set.contains(a)){
set.remove((Integer)a);
}else{
set.add(a);
}
}
num = !set.isEmpty()? (int) set.iterator.next(): 0;
return set.isEmpty()?-1:num;
}
//example input/output
int[] a = {1,2,6,1,6}
// Should return 2 but get the wrong answer 1

LinkedHashSet which maintains insertion order should be used to track the first unique number
Another Set is needed to keep the duplicated values in the input array which can be detected using the fact that Set::add returns false when the element has not been actually added to the set. Then the duplicates array has to be removed from the set of input and the first remaining element is returned.
Also, it may be better to return null / Integer value instead of -1 which is more appropriate for returning an index in the input array/list when a requested value is not found.
That being said a solution may look as follows:
public static Integer firstUnique(int ... arr) {
Set<Integer> input = new LinkedHashSet<>();
Set<Integer> duplicates = new HashSet<>();
for (int x : arr) {
if (!input.add(x)) {
duplicates.add(x);
}
}
input.removeAll(duplicates);
return input.isEmpty() ? null : input.iterator().next();
}
Test:
System.out.println(firstUnique(1, 2, 6, 1, 4, 6)); // 2
System.out.println(firstUnique(1, 2, 6, 6, 1, 2)); // null

Related

Minimum number of Groups needed to convert the given Array to another Array

I have a given array with unique values say [1,4,3,2] and another desired array [1,2,4,3]
I want to cut this input array into a minimum number of pieces so I can convert that to the desired array by just re-arranging the cut pieces.
So for the input array [1,4,3,2] I can cut into pieces (1), (4,3), (2) and re-arrange like (1),(2),(4,3) to get the desired array [1,2,4,3]
Constraints:
Array values are unique.
The size of both arrays is the same and can be up to 1000.
Values in arrays are integers
So the answer is 3 pieces for this example.
Here is what I have tried:
public int process(int[] inp, int[] desired) {
int ans = 0;
for (int i = 0; i < inp.length; i++) {
if (inp[i] != desired[i]) ans++;
}
return ans;
}
My approach is not a correct one, as I am finding elements at each index to count the mismatches. What is the correct approach for solving this problem?
Since all elements in the given arrays are unique, we can store index the positions of elements in one of these arrays by generating a map of type Map<Integer,Integer> associating an array element (Key) with its index (Value).
Then iterate over the second array, searching for identical chunks.
For that, we need to maintain a variable prevIndex that would store the index occupied by the previous element in the first array. While iterating, we would need to check each next index in the first array (corresponding to the next element in the first array). If the next index differs from the previous only by one - the next element is proved to be a part of a chunk with identical order, and we're just incrementing prevIndex. Otherwise, increment the count of chunks.
public static int process(int[] arr1, int[] arr2) {
Map<Integer, Integer> indexByValue = mapIndices(arr2);
int count = 1;
int prevIndex = indexByValue.get(arr1[0]);
for (int i = 1; i < arr1.length; i++) {
int nextIndex = indexByValue.get(arr1[i]);
if (nextIndex == prevIndex + 1) {
prevIndex++;
} else {
prevIndex = nextIndex;
count++;
}
}
return count;
}
public static Map<Integer, Integer> mapIndices(int[] arr) {
return IntStream.range(0, arr.length)
.boxed()
.collect(Collectors.toMap(
i -> arr[i],
Function.identity()
));
}
main()
public static void main(String[] args) {
System.out.println(process(new int[]{1, 4, 3, 2}, new int[]{1, 2, 4, 3}));
}
Output:
3

Removing duplicates and printing different integer pairs that result in the same product

I have been trying to code a program where the user has to input some integers in an array, without duplicates (e.g. having two 3's), and then the program finds pairs of integers that result in the same product within the given array. Moreover, the program cannot display two equal pairs (e.g. 40x1 and 1x40). This is what I have managed to do so far, not sure if it is memory efficient.
import java.util.*;
public class Question1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] list = new int[10];
System.out.println("Input the numbers to check if there are any common products");
for (int k = 0; k < list.length; k++) {
list[k] = sc.nextInt();
}
System.out.println(Arrays.toString(list));
showProducts(list, 10);
}
static void showProducts(int[] list, int product) {
Set<Integer> hset = new HashSet<Integer>();
int pairedNum = 0;
for (int element : list) {
if (product % element == 0) {
hset.add(element);
}
}
for(Integer element : hset) {
pairedNum = product / element;
if(hset.contains(pairedNum)) {
System.out.println(element + pairedNum);
}
}
}
}
You are almost there, you need to first change the way you print your pairs since you are adding the element + pairedNum before it is transformed into a string to be printed.
The second thing is that you need to remove the quotient once is used, since it cannot be used again. The thing is that you cannot do that while iterating using the for (value : values), you do that using the java iterator.
Modify the second for in the showProducts method:
Iterator<Integer> it = hset.iterator();
while(it.hasNext()) {
int element = it.next(), pairedNum = product / element;
if(hset.contains(pairedNum)) {
it.remove();
System.out.println(String.format("{%s, %s}", element, pairedNum));
}
}
If you don't want to use Iterator you can use, you can use another set and add the used elements (product / element) in it and then check the first contains is true and the second false.
You can also use an ordered set TreeSet and use the current contains() and also if element < (product / element), which would be true when element is less than the searched number and it hasn't been used.
For this input {1, 2, 10, 1, 5, 99, 100, 101, 59, 89} and product=10 it will print {1, 10} and {2, 5}.
Moreover, the program cannot display two equal pairs (e.g. 40x1 and
1x40).
Well, but it does. You could just check for element <= pairedNum (or the other way round, but just one way) to avoid it.
On the other hand: If you make the product flexible,
showProducts (list, sc.nextInt ());
and the user (or whoever) chooses a square root (like 9) and a single 3 is in the input (and duplicates where eliminated by the set) - should it print/count as a solution?
Printing only if element < pairedNum (or >) would solve that issue.
Else, it is a pretty fast and efficient solution. But it would be a bit faster and meanwhile solve the problem of symmetric pairs, if you would test for the pairedNum immediately while inserting the factor:
static void showProducts(int[] list, int product) {
Set<Integer> hset = new HashSet<Integer>();
int pairedNum = 0;
for (int element : list) {
if (product % element == 0) {
pairedNum = product / element;
if (hset.contains (pairedNum)) {
System.out.println (element + "*" + pairedNum);
}
hset.add(element);
}
}
}
Note, that you have to split the output, for example like System.out.println (element + "*" + pairedNum); - else the ints get added and then printed (19, 46).
echo 12 4 8 5 2 11 16 20 34 44 88 | java Question1
Input the numbers to check if there are any common products
[12, 4, 8, 5, 2, 11, 16, 20, 34, 44]
11*8
44*2

Array of unique numbers

I'm trying to write a function that returns an array of unique numbers. The array must be sorted. The function works fine when there are more than just one element, but it doesn't work for when there is just one element in the array. The function should return the only element found in that array, but instead, it is returning an empty array.
Why is this happening?
Array must be sorted for numUnique and removeDuplicates
public static int numUnique (double[] list){
int uniques = 1;
int i = 1;
if(list.length == 0) return 0;
while(i<list.length){
if(list[i] != list[i - 1])
uniques++;
i++;
}
return uniques;
}
public static double[] removeDuplicates(double[] list){
double[] arrayOfUniques = new double[numUnique(list)];
if(list.length == 0) return arrayOfUniques;
int uniques = 1;
int i = 1;
arrayOfUniques[0] = list[0];
while(i < list.length){
if(list[i] != list[i - 1])
arrayOfUniques[uniques++] = list[i];
i++;
}
return arrayOfUniques;
}
Array:
double[] a = {11,11,21,31,41,41,51,61,61,61,71,71,81,91,91,100,100};
Output:
Unique numbers: 9
Array of uniques:
[11.0, 21.0, 31.0, 41.0, 51.0, 61.0, 71.0, 81.0, 91.0]
But it doesn't work when the array just has one element:
double[] a = {11};
Output:
Unique numbers: 0
Array of uniques:[]
Try this.. Convert your array to list & convert list to set
Set not have duplicate values.. simple
List<String> myList= Arrays.asList(a);//convert array to List
Set<Double> uniqueSet = new HashSet<Double>(myList);//you get unique values
If you want to Set to Array try this https://stackoverflow.com/a/5982478/3879847
public List<Double> removeDuplicates (List<Double> list){
// add elements to al, including duplicates
Set<Double> hs = new HashSet<Double>();
hs.addAll(list);
list.clear();
list.addAll(hs);
return list;
}
Actually, it doesn't work for arrrays having only one unique element either (e.g. it returns empty array for new double[]{11.0,11.0, 11.0}). I would recommed a simpler approach using Java 8's stream:
public static double[] removeDuplicates(double[] list){
LinkedHashSet<Double> uniqueElements = Arrays.stream(list)
.boxed()
.collect(Collectors.toCollection(LinkedHashSet::new));
return uniqueElements.stream().mapToDouble(d -> d).toArray();
}
With Java 8 we get Stream, so how to use it in this case :
public static void main (String[] args) {
double[] a = {11,11,21,31,41,41,51,61,61,61,71,71,81,91,91,100,100};
//Way 1 just to print the unique element
DoubleStream.of(removeDuplicate(a)).forEach(System.out::println);
//Way 2 to save the update the array with only the unique element
a = removeDuplicate(a);
}
private static double[] removeDuplicate(double[] a) {
return DoubleStream.of(a).distinct().sorted().toArray();
}
In the main, we're just calling the method, and then read the result and print the values
In the removeDuplicate method : we create Stream of the values, then we keep only the different elements, then we sort them, and them we come back to an array (to follow your wishes)
If you're not sure to HAVE to use array, better use List every time ;)

How to insert an integer into a sorted List in Java?

I would like to write a function insertAndSort() that will take as parameter an Integer "num" and a List of integers "list". Let's assume the List lust is already sorted.
The algorithm is supposed to work this way :
Add "num" to L and keep the List sorted after "num" has been added.
Return the sorted List.
WARNING : We do not know whether the list is sorted ASC or DESC. And that is precisely my problem !
Example :
if "num" = 4 and L = {1, 3, 5, 7}, then the final sorted List is {1, 3, 4, 5, 7}
if "num" = 4 and L = {7, 5, 3, 1}, then the final sorted List is {7, 5, 4, 3, 1}
I can not use sorting API such as Collections.sort or Collections.reverseOrder etc...
So far, I've produced this code :
public static void main(String[] args) {
int num = 4;
List<Integer> myList = Arrays.asList(1, 3, 5, 7);
List<Integer> newList = new ArrayList<Integer>();
newList = insertAndSort(myList, num);
System.out.println(newList);
}
public static List<Integer> insertAndSort(List<Integer> list, int num) {
List<Integer> listSecond = new ArrayList<Integer>(list.size()+1);
for(int i = 0; i <= list.size() ; i++) {
if(num < list.get(i)) {
if(!listSecond.contains(num)){
listSecond.add(num);
} else {
listSecond.add(list.get(i-1));
listSecond.add(list.get(i));
break;
}
} else {
listSecond.add(list.get(i));
}
}
return listSecond;
}
The problem is that this seems to be working with a single type of List: the ascending one.
When I take a sorted descending List, it does not work anymore.
Do you have any idea to make this work with both type of List ?
Thanks and Regards.
First, you need to detect the sort order in the existing list.
If the list is empty, you don’t need to care, just add your element, you cannot break any existing sort order.
If the list contains one element, I cannot tell you what to do. Options include tossing a coin and throwing an exception, but there are more.
If the list contains two or more elements, iterate through them except for the last element, each time comparing the current element with the element in the next higher index. As soon as you encounter a pair of elements that are not equal, you know the sort order. If you only encounter equal elements, all the elements in the list are equal, and again I cannot tell you what to do.
Once you’ve detected a sort order, I suggest a big if-else statement to handle the two cases separately.
You already have the code for the ascending case. Except it doesn’t always work. If I try to insert 4 into { 1, 3, 5 }, I get an ArrayIndexOutOfBoundsException. If I try with { 1, 3, 5, 7, 9 }, the 9 is lost. You should probably find a fix for that.
You should be able to handle the descending case similarly to the ascending case. Only use num > list.get(i) instead of num < list.get(i).
First, you need to check whether the list is sorted in ASC or DESC. That's easy: compare the 1st two elements look for two consecutive non-identical elements and see whether the first one is greater or the second one (thanks tom for correcting the mistake).
Now, for a list sorted in ascending order, convert list to an ArrayList. Then, add num to the ArrayList and convert it to a TreeSet, since TreeSets sort their elements in ascending order. Finally, reassign the ArrayList to hold the contents of the TreeSet and return it.
For a list sorted in descending order, first, convert the TreeSet to an ArrayList, then iterate over this ArrayList in reverse order and add the elements to a new ArrayList. Return the second ArrayList.
public List<Integer> insertAndSort(List<Integer> list, int num){
ArrayList<Integer> a = new ArrayList<Integer>(list);
a.add(new Integer(num));
TreeSet t = new TreeSet(a);
a = new ArrayList<Integer>(t);
int l = list.size();
for(int i=1; i<l; i++){
if(list.get(i) != list.get(i-1))
break;
}
if(list.get(i) > list.get(i-1)) //ASC
return a;
else{ //DESC
ArrayList<Integer> a2 = new ArrayList<Integer>();
for(int i = a.size() - 1; i >= 0; i--)
a2.add(a.get(i));
return a2;
}
}
I would proceed with something like the code below. A few remarks :
it is possible to decide sort order if and only if there is at least 1 element AND first and last elements have different values.
the line int oo = list.get(p2) - list.get(p1); compute the
difference between the last and first element (negative = DESC,
positive = ASC)
the variable ox is positive if and only if the
added element is after the element pointed by the variable p3
whatever the order.
the position is found by a binary search algorithm by choosing p3 between p1 and p2 and deciding if the element is before or after p3.
This is not fully tested bu works with the examples you gave :
// static List<Integer> list = new ArrayList<>(Arrays.asList(7, 5, 3, 1));
static List<Integer> list = new ArrayList<>(Arrays.asList(1, 3, 5, 7));
static void add(int x)
{
// fixed code below (see Ole V.V. comment)
}
static public void main(String [ ] args)
{
add(4);
for(int x: list)
System.out.println(x);
}
EDIT: to be complete (see Ole V.V. comments)
When the new element is to be inserted before the first or after the last, two simple O(1) tests may be performed ; the general case has O(log N) complexity, this is more efficient than traversing the entire list which is O(N).
Be carefull too when comparing elements to deduce sort order, there may be several equal values ; the best is to compare the first and the last elements, this is O(1) - again better than O(N).
Algorithmic complexity is an important matter (to me) and was the primary aim of my post. The code of the add function becomes :
static void add(int x)
{
int p1 = 0;
int p2 = list.size() - 1;
if(list.size() == 0 || list.get(p1) == list.get(p2))
throw new IllegalStateException("Unable to decide sort order");
int oo = list.get(p2) - list.get(p1);
if(oo * (x - list.get(p1)) <= 0) // new element is before first
list.add(0, x);
else if(oo * (x - list.get(p2)) >= 0) // new element is after last
list.add(x);
else
{
while (p1 + 1 < p2)
{
int p3 = (p1 + p2) / 2;
int ox = (x - list.get(p3)) * oo;
if(ox >= 0) // new element is after p3
p1 = p3;
if(ox <= 0) // new element is before p3
p2 = p3;
}
list.add(p2, x);
}
}
Note : there may be still some undealed side effects. If the asker is interested, I am ready to give further help - just ask.
Your code and what you say is two different things.
Based on code you made, I see that List can't contain more than one instance of same value.
If thats the case, main method should look like this:
public static void main(String[] args){
int num = 4;
List<Integer> myList = Arrays.asList(1, 3, 4, 5, 7);
List<Integer> newList;
if (!myList.contains(num)) {
newList = insertAndSort(myList, num);
} else {
newList = copyList(myList);
}
System.out.println(newList);
}
if thats not the case:
public static void main(String[] args){
int num = 4;
List<Integer> myList = Arrays.asList(1, 3, 4, 5, 7);
List<Integer> newList;
newList = insertAndSort(myList, num);
System.out.println(newList);
}
Methods that main method use:
Assuming we can only know how list is sorted by values in list, we have to decide default sort method when there is only 1 value or all values are same. I picked ASC as default. If elements can be of same value and we are certain list is sorted its best to compare lowest value in list with the highest one. With this approach we have to compare 2 elements only once.
So method to see if list is sorted DESC would look like this:
private static boolean isDesc(List<Integer> list) {
return list.get(0) > list.get(list.size() - 1);
}
If method returns true its sorted DESC. With returning false its ASC. If we want to change default value, when values don't tell us how its sorted and we want it to be DESC we would replace '>' sign with '>='
private static boolean isDesc(List<Integer> list) {
return list.get(0) >= list.get(list.size() - 1);
}
Code for as you called it insertAndSort:
public static List<Integer> insertAndSort(List<Integer> list, int num) {
List<Integer> listSecond = new ArrayList<Integer>(list.size() + 1);
boolean isDescSortOrder = isDesc(list);
if (isDescSortOrder) {
int i = 0;
while ((i < list.size()) && (list.get(i) > num)) {
listSecond.add(list.get(i));
i++;
}
listSecond.add(num);
while (i < list.size()) {
listSecond.add(list.get(i));
i++;
}
} else { // is asc sort order
int i = 0;
while ((i < list.size()) && (list.get(i) < num)) {
listSecond.add(list.get(i));
i++;
}
listSecond.add(num);
while (i < list.size()) {
listSecond.add(list.get(i));
i++;
}
}
return listSecond;
}
Depending on how its sorted code got devided into 2 blocks. As we don't know on each iteration will be right place to insert our num value its better to use while loop. Using for loop and checking everytime if num value already exists in list is counterproductive. On top of that I dunno if your application should allow same values in the list. If I assume it should, you can't add num value if it already existed in the list with for loop and checking with contains every iteration.
And just to copy the table when list already has the element and we don't want to add elements that are already incuded:
public static List<Integer> copyList(List<Integer> list) {
List<Integer> listSecond = new ArrayList<Integer>(list.size());
for (int i = 0; i < list.size(); i++) {
listSecond.add(list.get(i));
}
return listSecond;
}
Also based on good programming practices I'd recommend to name methods based on what they do.
Calling method insertAndSort is asking for trouble. If I seen it, I'd say it alters list we are giving in parameter. Then it sorts it.
So putting unsorted list in it would give us unwanted outcome.
And I'd still ask myself why does it return a List when we are inserting an item to already existing list? I'd rather name it:
public static List<Integer> copySortedWithNewValue(List<Integer> sortedList, int newValue)
Try to use PriorityQueue. This collection contains a sorted sequence of elements which could be duplicated.

Randomly prints elements in an array

Is there a way where you can use Math.random to prints the element in a given array?
int[] list = new int[] {1,2,3};
So the output will be like
2,1,3
or
3,1,2
or
2,3,1
Perhaps you can approach it by shuffling your array then print it. If the original should not be modified, you can make a copy and then shuffle the copy.
There are well-known algorithms for shuffling array (or a deck of cards). One can be found here. An implementation in java looks like this:
static void shuffleArray(int []array) {
int length = array.length;
for (int i = length -1; i > 0; i--) {
// generate a random 0 <= j < i
int j = (int)(Math.random() * i);
// swap elements at i and j
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
The approach proposed in most answers is extremely inefficient, as it works in O(N2) time. Think about it: at first you'll generate unused indexes with one attempt, but closer to the end, when almost all array is processed, it will require nearly N steps to generate next unused index.
The optimal O(N) approach is to create shuffled array of indexes (0..N) where each index appears only once and then process your original array in the order of shuffled indexes. Each step requires O(N) time, so the whole algorithm is O(N).
int[] input = new int[]{5, 4, 3, 6, 2, 1};
int []indices = new int[input.length];
//Fisher-Yates shuffle
Random rnd = new Random();
for (int i = 0; i < indices.length; i++) {
int j = rnd.nextInt(i + 1);
indices[i] = indices[j];
indices[j] = i;
}
for (int i : indices) {
System.out.println(input[i]);
}
I didn't use Collections.shuffle, as it would require usage of Collection and thus wrapped Integer elements, which is very inefficient comparing to the plain int array.
Also, if you are ok with modifying your original array, you can just shuffle it in place (using the same Fisher-Yates shuffle) and then consume it while traversing.
UPD: Replaced shuffling array of indices with shuffled initialization.
Since you have java 8, you can take advantage of the beautiful Stream API.
In short, you can do:
new Random().ints(1, 500).limit(500).forEach(p -> System.out.println(list[p]));
Where 1 is the lowest int generated (inclusive) and 500 is the highest (exclusive). limit means that your stream will have a length of 500, maybe in that argument you want to put list.length.
For your case:
int[] list = new int[] {1,2,3,4,5,6};
new Random().ints(0, list.length).limit(10).forEach(p -> System.out.println(list[p]));
Prints: 5 2 5 4 6 3 3 5 6 4 (Obviously will not print the same numbers for you)
Create a random integer that may be as high as the length of the array - 1. If the random integer is equal to a previous used random integer -- known by storing used integers in an array -- create a new random integer. Otherwise, print the string correlated with that index specified by the random integer. If the length of the array storing the used random integers is equal to the length of the array of strings, stop the process.
This should print all your strings only once each and randomly.
Here is the solution
public static void main(String[] args) {
int[] list = new int[] { 1, 2, 3 };
int[] aux = new int[list.length];
int countTimes = 0;
while (countTimes < list.length) {
int position = new Random().nextInt(list.length);
if (aux[position] != list[position]) {
System.out.println(list[position]);
aux[position] = list[position];
countTimes++;
}
}
}
As I said in the comments. This answer will work.. All you need to do is track the indices that it accessed so you don't repeat them or remove that element from the array.
void printRandom(int[] array) {
if (array.length == 0)
return;
Random rand = new Random();
int rnd = rand.nextInt(array.length);
int element = array[rnd];
array = ArrayUtils.removeElement(array, element);
System.out.print(element);
printRandom(array);
}
Just repeat this process until all elements are removed. Obviously add checks to prevent errors and keep in mind I haven't used JAVA in a long time so post back if you have issues!
Lastly keep in mind this deletes the array so you may want to wrap this code in a function and then copy the array as a local variable so you can reuse the original as needed
In this case we can print random value from array using like below :
Steps:
Create list object of Integer to hold printed indices
Get random number and check whether this index is already printed or not
if not printed then add it in list and print value from array using this index
if list size and array length is equal then terminate the loop
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class RandomIndices {
public static void main(String[] args) {
int[] list = new int[]{1, 2, 3};
Random random = new Random();
List<Integer> randomIndices = new ArrayList<>(); //to hold indices which are already printed
boolean isRemain = true;
while (isRemain) {
int randomIndex = random.nextInt(list.length);
if (!randomIndices.contains(randomIndex)) { //check random index value of array is printed or not
randomIndices.add(randomIndex);
System.out.println(list[randomIndex]);
}
if (randomIndices.size() == list.length) {
isRemain = false;
}
}}
}
Implement a simple "do while" statement to prevent duplicate numbers from showing up out of your array (I used a StringArray - but an IntegerArray would work the same way - as a side note, I can place the complete code up here but didn't want to do so if it didn't apply. I use a drop-down to select how many random words to generate - then display that set of true RANDOM words (non-repeated):
final Random rand1 = new Random();
final Random rand2 = new Random();
final int rndInt1 = rand1.nextInt(getResources().getStringArray(R.array.words).length);
int rndInt2 = rand2.nextInt(getResources().getStringArray(R.array.words).length);
if (rndInt1 == rndInt2){
do {
rndInt2 = rand2.nextInt(getResources().getStringArray(R.array.words).length);
}while (rndInt1 == rndInt2);//if indexes are equal - re-run the array search
}
outString = getResources().getStringArray(R.array.words)[rndInt1];
outString += ", " + getResources().getStringArray(R.array.words)[rndInt2];//concatenate the list
textWord = (TextView) findViewById(R.id.textWords);//An empty text field in my layout
textWord.setText(outString);//Set that empty text field to this string of random array elements

Categories

Resources