I'm trying to rid of duplicates for my Bingo Program so I created an ArrayList and I want to remove the integer that is printed out in the loop. I think the loop I'm using is wrong but I'm not sure.
List<Integer> list = new ArrayList<Integer>();
for(int i = 1; i <= 75; i++){
list.add(i);
}
for (Integer s : list) {
Collections.shuffle(list);
System.out.println(s);
list.remove(//);
}
Do like this.
for (Iterator<Integer> iterator = list.iterator(); iterator.hasNext();) {
Integer s = iterator.next();
System.out.println(s);
iterator.remove();
}
If you're removing all the integers, it would be a lot easier to just do this:
Collections.shuffle(list);
for (Integer s : list) {
System.out.println(s);
}
list = new ArrayList<Integer>();
Or, if you really want to keep the same list instance:
Collections.shuffle(list);
for (Integer s : list) {
System.out.println(s);
}
list.clear();
Just creating a new array list is more efficient because it allows the garbage collector to just collect the entire old list, rather than removing the entries one by one. If you have multiple references to the same instance, however, then you'd need to actually clear the list instead.
Also note that the shuffle call has been moved outside the loop. Once you've done one shuffle, the list is already randomized, so shuffling again is kind of pointless... If you really want a "better" shuffle, you could do something like call shuffle 7 times before the first loop.
I would suggest using a HashSet<Integer> which doesn't allow duplicates:
Set<Integer> set = new HashSet<Integer>();
Are you sure this is not what you need?
private static final int NUM_BALLS = 75;
List<Integer> list = new ArrayList<Integer>();
for(int i = 1; i <= NUM_BALLS; i++){
list.add(i);
}
Collections.shuffle(list);
while (!list.isEmpty()) {
Integer s = list.remove(list.size() - 1); // for better performance as noted by #Holger
System.out.println(s);
}
use following code
Random generate = new Random();
Set<Integer> set = new HashSet<Integer>();
for(int i = 1; i <= 75; i++){
set.add(generate.nextInt(75));
}
Iterator<Integer> iterator = set.iterator();
while(iterator.hasNext()){
System.out.println(iterator.next());
iterator.remove();
}
System.out.print(set.size());
Related
I have the following Java code:
List<List<Integer>> list1 = new ArrayList<List<Integer>>(rankings);
for (int i = k + 1; i < rankings.size(); i++) {
for (int j = 0; j < rankings.get(0).size(); j++) {
int index = rankings.get(i).indexOf(j);
list1.get(i).set(index, map.get(j));
}
}
// ...
int newrank = sort(list1.get(i), 0, list1.get(i).size() - 1); // sorts list1
list1 came out sorted, but rankings came out sorted as well. How can I prevent this?
All I wanted to do is to create a duplicate of rankings so that the original copy won't be affected while I sort the temporary, copied array.
Thanks in advance.
new ArrayList<>(list) copies the reference here, not cloning the objects, every amends made in one element will affect both lists.
You can add the elements manually to clone it:
for (List<Integer> intList: rankings) {
List<Integer> someIntCopy = new ArrayList<>();
someIntCopy.addAll(intList);
list1.add(someIntCopy);
}
I want to store unique lists, so I am using HashSet. But, I am not getting desired output. Here is my code, Could you tell me what is going wrong?
public List<List<Integer>> threeSum(int[] nums) {
Set<List<Integer>> res = new HashSet<>();
for(int i = 0; i< nums.length; i++){
int target = 0-nums[i];
Set<Integer> neg = new HashSet<>();
for(int j = i+1 ; j<nums.length; j++){
int rem = target - nums[j];
if(neg.contains(nums[j])){
res.add(new ArrayList<>(Arrays.asList(nums[i], rem, nums[j])));
}
else{
neg.add(rem);
}
}
}
System.out.println(res);
return new ArrayList<>(res);
}
Here my nums is [-1,0,1,2,-1,-4].
My output is [[-1,2,-1],[0,1,-1],[-1,0,1]]. Why am I getting both [0,1,-1] and [-1,0,1] into res as both contain the same elements. I what only one of these? What should I do?
From the Javadoc of List.equals:
Returns true if and only if the specified object is also a list, both lists have the same size, and all corresponding pairs of elements in the two lists are equal.
So, [0,1,-1] and [-1,0,1] aren't equal, despite containing the same elements, because they aren't in the same order.
The easiest way to solve this would be to sort the list:
res.add(Stream.of(nums[i], rem, nums[j]).sorted().collect(toList()));
You can sort the List before adding to the Set so they will be in the same order.
I have project to create storage, and i am using ArrayLists for every room.
Is there any solution to create more than one ArrayList in loop ?
I just want to reduce amount of code.
Quotes/brackets and other stuff arent working. Is there any solution for beginner?
I was trying something like this.
for(int i=0; i<10; i++}{
ArrayLists list[i] = new ArrayLists();
}
ArrayList list0 = new ArrayList();
ArrayList list1 = new ArrayList();
ArrayList list2 = new ArrayList();
//up to 10
use arrays.fill and get rid off the loop
ArrayList<Integer>[] al = new ArrayList[5];
Arrays.fill(al, new ArrayList<Integer>());
I am not entirely sure what you want to achieve, but you can have a list of lists like so:
ArrayList<ArrayList<>> listOfLists = new ArrayList<>();
for (int i = 0; i < 10; i++) {
listOfLists.add(new ArrayList<String>());
}
List<List<Integer>> lists = new ArrayList<List<Integer>>();
for (int i = 0; i < 5; i++) {
List<Integer> list = new ArrayList<>();
lists.add(list);
}
Will create Multiple List inside a List.
I want to iterate over java set by providing some range. like I can do it for list..
//for list
int startRange=3;
int endRange = 5;
List list = myList();
for (int i=startRange;i<endRange;i++){
System.out.println(list.get(i));
}
Now I wanted to iterate on below java set like above in startRange and endRange
Set<String> set = new java.util.HashSet<String>(list);
Thanks.
int count =0, min= 3 , max = 5;
Set<String> s = new HashSet<String>();
Iterator<String> itr = s.iterator();
while (itr.hasNext()) {
if ( count > min && count < max ) {
//Do something
}
count++;
}
You can use a LinkedHashSet and maintain the order.
See my comment below.
#alfasin is correct; List encompasses a total ordering on its elements, but HashSet has no such thing... PS you should use List.listIterator in your original code.
That being said, there is something similar for other kinds of Sets that do impose a total order -- SortedSets. There exists an analogous subSet, as well as headSet and tailSet which have similar functions. These also exist in NavigableSet along with other methods like ceiling, floor, etc.
Note that the orders here are not that much alike. List order is essentially arbitrary, whatever the user intended, while SortedSets order using a comparison sort.
Set is not ordered use Class LinkedHashSet
//You can change the Set to Array and then iterate over a range.
Set<Integer> set = new TreeSet<Integer>();
for (int i = 0; i <= 100; i++) {
set.add(i);
}
Object[] array = set.toArray();
for (Integer i = 0; i < 100; i++) {
System.out.print(array[i] + " ,");
}
I am new to programming so I'm sorry if this is to "noobish" a question.
I have an array containing values which describe the frequency of occurrences - e.g. {4,5,2,7,8,,15,16,12,4,2,7,6,22}. How do I extract the values that are higher than 6 and present them in a new array?
ArrayList<Integer> newList = new ArrayList<Integer>();
for (Integer i: array) {
if (i > 6) {
newList.add(i);
}
}
Integer[] newArray = newList.toArray(new Integer[0]);
Hope this helps.
int j=0;
for(i=0;i<array.length;i++)
{
if(array[i]>6)
{
newArray[j]=array[i];
j++;
}
this is the way you can do it or you can proceed.