Foreach with multidimensional list - java

List< List<Integer> > arr = new ArrayList< List<Integer> >();
// filling the array
for(List<Integer> values in arr) {
System.out.println(values[0] + values[1]);
}
Why doesn't it work? It displayes an error that ";" is expected, and that it cannot find the symbol. Simple for doesn't work either:
for(int i = 0; i < arr[]; i++) {
}

That is not the correct syntax for the for each loop in Java. Also, you cannot use the [index] notation for a List. That syntax is reserved for arrays. Here is the proper way to iterate using for each in Java.
for(List<Integer> values : arr) {
System.out.println(values.get(0) + values.get(1));
}
For the second half of your question, you should be iterating from 0 towards the size() of the List.
for(int i = 0; i < arr.size(); i++) {
}

Related

Is there a way to iterate through a 2d list where it'll access the elements in order by first index, second, etc

List<List<Integer>> myList = new ArrayList<>(3);
for(int i=0; i < 3; i++) {
myList.add(new ArrayList());
}
myList.get(0).add(1); // 0,0
myList.get(0).add(4); //0,1
myList.get(1).add(2); // 1,0
myList.get(1).add(5); // 1,1
myList.get(2).add(3);// 2,0
myList.get(2).add(6); //2,1
myList.get(2).add(7); //2,3
for(int i =0; i<myList.get(i).size(); i++){
for(int j=0; j<myList.size(); j++){
System.out.println(myList.get(j).get(i));
}
}
I cant figure out how to iterate through the list on a index based, with different lengths on each list. My code above only works if all lists are the same size.
Ideal output would be:
1
2
3
4
5
6
7
But I cant figure out how to print out 7 since that list is a different length. This might be a very simple solution and ill probably feel dumb after. Thanks guys
To iterate over all elements of List of Lists you need to iterate in the first for-loop over the outer List, and in the second for-loop over the inner loop at that index. There are several possibilities to achieve the iteration over all elements, as you will see in the following examples.
(Your code would also produce a IndexOutOfBoundsException for the last entry).
Iterating through a List of Lists
Option 1 (your code corrected)
for (int i = 0; i < myList.size(); i++) { // i represents index of outer List
for (int j = 0; j < myList.get(i).size(); j++) { //j represents index of the inner list at index i
System.out.println(myList.get(i).get(j));
}
}
Option 2 (using for-each loop)
for (List<Integer> innerList : myList) {
for (Integer currentPosition : innerList) {
System.out.println(currentPosition);
}
}
Option 3 (using streams)
myList.stream()
.flatMap(Collection::stream)
.forEach(System.out::println);
}
Edit due to comment: added traverse method for wanted output
If you want to print out all first entries of the inner lists first, a possibility would be to traverse your List<List<Integer>> with a method like this (method is generic, would also work with other classes):
private static <T> List<List<T>> traverse(List<List<T>> input) {
List<List<T>> result = new ArrayList<>();
for (int i = 0; i < input.size(); i++) {
for (int j = 0; j < input.get(i).size(); j++) {
if(result.size() <= j) {
result.add(new ArrayList<>());
}
result.get(j).add(input.get(i).get(j));
}
}
return result;
}
In your method then just create a new List<List<Integer>> like this and iterate over this new list of lists:
List<List<Integer>> myListTraversed = traverse(myList);

How to break an ArrayList into groups?

I am trying to implement this method:
public ArrayList<ArrayList> groupWords(ArrayList<String> scrambledWords, int groupNumber);
The method takes an ArrayList of Strings and a number that represents the number of words in each group as parameters and then returns an ArrayList made of ArrayLists that contain groups of words according to the groupNumber parameter. For example, there is an ArrayList of 20 strings and I want to group that ArrayList into groups of 5 so I call the method like this:
ArrayList<ArrayList> groupedWords = groupWords(ArrayList, 5);
I am pretty sure that I need to have a for loop with another for loop nested inside, but I am not sure how to implement it.
How do I implement this method?
With Guava:
List<List<String>> groupedWords = Lists.partition(words, 5);
Something like this should work:
ArrayList<ArrayList<String>> grouped = new ArrayList<>();
for(int i = 0; i < words.size(); i++) {
int index = i/groupSize;
if(grouped.size()-1 < index)
grouped.add(new ArrayList<>());
grouped.get(index).add(words.get(i));
}
I haven't tested this code but basically I'm using the fact that integer division is always rounding to the next lowest Integer.
Example: 4/5=0.8 and is rounded to 0.
public ArrayList<ArrayList> groupWords(ArrayList<String> scrambledWords, int groupNumber){
int arraySize = scrambledWords.size();
int count = 0;
ArrayList<ArrayList> result = new ArrayList<>();
ArrayList<String> subResult = new ArrayList<>();
for(int i = 0 ; i < arraySize; i++){
if(count == groupNumber){
count = 0;
result.add(subResult);
subResult = new ArrayList<>();
}
subResult.add(scrambledWords.get(i));
count++;
}
return result;
}
This is simple Java Collections Soultion.
Suggestion : As a return type you should use ArrayList<ArrayList<String>>, and this should be the type for result also.

array of arraylist type error

void radix(int[] arr, int maxDigit){
int exp = 1;
for (int i=0; i<maxDigit; i++) {
ArrayList[] bucket = new ArrayList[10];
for (int n=0; n<10; n++){
bucket[n] = new ArrayList<Integer>();
}
for (int j=0; j<arr.length; j++){
bucket[(arr[j]%exp)%10].add(arr[j]);
}
int ind=0;
for (int k=0; k<10; k++){
for (int n : bucket[k]){
arr[ind] = n;
ind++;
}
}
exp*=10;
}
}
above is my attempt for a radix sort in java.
I keep having an error at this for loop:
for (int n : bucket[k])
Saying that Object can't be converted to int.
However, I declared the array to have an arrayList as its element,
so bucket[k] should be an arraylist.
Can anyone please explain why this happens and how I could get around this problem?
I would appreciate any help. Thanks
You have an ArrayList<Integer>. Try using for(Integer n : bucket[k])
You also need to provide a type for ArrayList. Try ArrayList<Integer>[] bucket = new ArrayList[10];
ArrayList[] bucket = new ArrayList[10];
This does not have the type parameter.
And you should avoid using inflexible, low-level constructs like arrays anyways.
Try to actually express your intents in your code with identifiers and to use first-class objects for all of your key concepts.
You have an ArrayList not ArrayList.
Change:
for(int n : bucket[k])
To:
for(Integer n : bucket[k])
You also need to type your ArrayList declaration with:
ArrayList<Integer>[] bucket = new ArrayList<Integer>[];

Delete duplicates in array, Java [duplicate]

This question already has answers here:
How to efficiently remove duplicates from an array without using Set
(48 answers)
Closed 8 years ago.
I have written a method to count the number of occurrences of the words in a word file. Prior, in another method, i have sorted the words to appear in alphabetical order. There for a sample input into this method will look like this:
are
away
birds
birds
going
going
has
My question is.. How do i delete the repeated occurrences in this method? (after counting ofcoz) I have tried to use another string array to copy the unique ones into that string array, but i get a null pointer exception.
public static String[] counter(String[] wordList)
{
for (int i = 0; i < wordList.length; i++)
{
int count = 1;
for(int j = 0; j < wordList.length; j++)
{
if(i != j) //to avoid comparing itself
{
if (wordList[i].compareTo(wordList[j]) == 0)
{
count++;
}
}
}
System.out.println (wordList[i] + " " + count);
}
return wordList;
}
Any help will be much appreciated.
Oh, and my current output looks something like this:
are 1
away 1
birds 2
birds 2
going 2
going 2
has 1
I would prefer using Map to store word occurrence. Keys in the map are stored in Set so it can't be duplicated. What about something like this?
public static String[] counter(String[] wordList) {
Map<String, Integer> map = new HashMap<>();
for (int i = 0; i < wordList.length; i++) {
String word = wordList[i];
if (map.keySet().contains(word)) {
map.put(word, map.get(word) + 1);
} else {
map.put(word, 1);
}
}
for (String word : map.keySet()) {
System.out.println(word + " " + map.get(word));
}
return wordList;
}
I already posted an answer on this question. Your question is almost identical - he was having problems creating another array and getting an NPE too.
This is what I came up with (assuming the array is sorted):
public static String[] noDups(String[] myArray) {
int dups = 0; // represents number of duplicate numbers
for (int i = 1; i < myArray.length; i++)
{
// if number in array after current number in array is the same
if (myArray[i].equals(myArray[i - 1]))
dups++; // add one to number of duplicates
}
// create return array (with no duplicates)
// and subtract the number of duplicates from the original size (no NPEs)
String[] returnArray = new String[myArray.length - dups];
returnArray[0] = myArray[0]; // set the first positions equal to each other
// because it's not iterated over in the loop
int count = 1; // element count for the return array
for (int i = 1; i < myArray.length; i++)
{
// if current number in original array is not the same as the one before
if (!myArray[i].equals(myArray[i-1]))
{
returnArray[count] = myArray[i]; // add the number to the return array
count++; // continue to next element in the return array
}
}
return returnArray; // return the ordered, unique array
}
Sample input/output:
String[] array = {"are", "away", "birds", "birds", "going", "going", "has"};
array = noDups(array);
// print the array out
for (String s : array) {
System.out.println(s);
}
Outputs:
are
away
birds
going
has

How to merge two arraylists into one in ascending order

I need to merge two lists into one, in ascending order, not duplicates, and I think my code is really close, I'm just missing something and I can't figure it out. As of now, my code is not working properly in my merge method. I think it has something to do with my loops, but I just can't work around it. My current method prints the new list, but it is not in perfect increasing order. I would appreciate any assistance in figuring out how to make this method print my merged list with ascending order using the contents of l1 and l2.
**Note: I cannot use any built-in array sorting methods.
Thanks!
import java.util.ArrayList;
import java.util.Random;
public class MergeLists {
public static ArrayList<Integer> merge(ArrayList<Integer> l1, ArrayList<Integer> l2){
ArrayList<Integer> mergedList = new ArrayList();
for (int j = 0; j < l1.size(); j++) {
if (l1.get(j) < l2.get(j)) {
mergedList.add(l1.get(j));
mergedList.add(l2.get(j));
} else {
mergedList.add(l2.get(j));
mergedList.add(l1.get(j));
}
}
for (int i = l2.size() - l1.size(); i < l2.size(); i++) {
mergedList.add(l2.get(i));
}
return mergedList;
}
public static ArrayList<Integer> makeRandomIncreasingList(int length) {
ArrayList<Integer> randomList = new ArrayList();
Random rand = new Random();
int inList = rand.nextInt(9) + 1;
int inList2 = rand.nextInt(9) + 1;
for (int i = 0; i < length; i++) {
randomList.add(inList);
inList = inList + inList2;
}
return randomList;
}
public static void doMergeTest() {
ArrayList<Integer> list1 = makeRandomIncreasingList(10);
ArrayList<Integer> list2 = makeRandomIncreasingList(20);
ArrayList<Integer> mergedList = merge(list1, list2);
System.out.println("List 1:" + list1);
System.out.println("List 2:" + list2);
System.out.println("Merged list:" + mergedList);
}
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
System.out.println("Performing merge test #" + (i + 1) + ":");
doMergeTest();
}
}
}
Remove duplicates
arrayList1.remove(arrayList2);
Then merge two arrayList:
arrayList1.addAll(arrayList2);
And Lastly sort the last
collections.sort(arrayList1);
Another way is to use SET: Set doesnt allow duplicates
(HashSet is faster depending on the List implementation class)
Set setmerge = new HashSet(list1);
setmerge.addAll(list2);
list1.clear();
list1.addAll(setmerge);
The first part of your merge() method seems ok, if you modify it a little bit. You need to be going through both lists in parallel, something like
int i = 0, j = 0;
for (; i < l1.size() && j < l2.size();)
And compare individual items and increment indices independently, as in
if (l1.get(i) < l2.get(j)) {
...
i++;
} else
...
j++;
}
The way you were doing it you were literally going in parallel, which is not always correct (think of lists [1 2 2] and [1 1 1] => your merge would look like [1 1 1 2 1 2])
Then, after your "parallel" for-loop (the one where you're iterating through both lists), one of your indices is always going to break your loop because it's at the end of its list. For in-order merging, I usually declare i, j outside the loop (you'll need then after your first for-loop, like above) and then do something like (in your notation):
for (int i1 = i; i1 < l1.size(); i1++) {
mergeList.add(l1.get(i1));
}
for (int i2 = j; i2 < l2.size(); i2++) {
mergeList.add(l2.get(i2));
}
After your first for-loop, you get to the end of exactly one of the lists (someone's going to break the loop), so exactly one of the above loops is going to get executed, and that will contain the remaining items, in order.
Edit: your last for-loop of the merge() method is not correct for your purpose.
You have assumed l2 items are always bigger than l1 items, since you are adding remainder of l2 items in the end of the list. You need to compare them with mergedList items and add them accordingly.

Categories