My sample Mongo doc is:
{
"_id" : ObjectId("51fa3d516bde1ca6f017a570"),
"digits":[
0,
1,
2,
3,
4,
5,
6
]
}
I want to append digits array with some extra values.
say:
int[] myIntArray = new int[3];
int[] myIntArray = {7,8,9};
How to append myIntArray to digits array in java
my java code is:
BasicDBList digits=(BasicDBList) cursor.next().get("digits");
MongoDB has the $push operator for that. You don't have to iterate over the collection to do it, you can do an update inside the database.
Written in Java, this would look something like
collection.update(...query expression...,
new BasicDBObject("$push",
new BasicDBObject("digits", value)));
This assumes that value is a single new value for the array. If you want to append all the elements of an array at once, you have to use the $each modifier (see above link for the details).
Related
I know how to do it normally, but I could swear that you could fill out out like a[0] = {0,0,0,0}; How do you do it that way? I did try Google, but I didn't get anything helpful.
Check out the Arrays.fill methods.
int[] array = new int[4];
Arrays.fill(array, 1); // [1, 1, 1, 1]
You can also do it as part of the declaration:
int[] a = new int[] {0, 0, 0, 0};
Arrays.fill(). The method is overloaded for different data types, and there is even a variation that fills only a specified range of indices.
In Java-8 you can use IntStream to produce a stream of numbers that you want to repeat, and then convert it to array. This approach produces an expression suitable for use in an initializer:
int[] data = IntStream.generate(() -> value).limit(size).toArray();
Above, size and value are expressions that produce the number of items that you want tot repeat and the value being repeated.
Demo.
Arrays.fill(arrayName,value);
in java
int arrnum[] ={5,6,9,2,10};
for(int i=0;i<arrnum.length;i++){
System.out.println(arrnum[i]+" ");
}
Arrays.fill(arrnum,0);
for(int i=0;i<arrnum.length;i++){
System.out.println(arrnum[i]+" ");
}
Output
5 6 9 2 10
0 0 0 0 0
An array can be initialized by using the new Object {} syntax.
For example, an array of String can be declared by either:
String[] s = new String[] {"One", "Two", "Three"};
String[] s2 = {"One", "Two", "Three"};
Primitives can also be similarly initialized either by:
int[] i = new int[] {1, 2, 3};
int[] i2 = {1, 2, 3};
Or an array of some Object:
Point[] p = new Point[] {new Point(1, 1), new Point(2, 2)};
All the details about arrays in Java is written out in Chapter 10: Arrays in The Java Language Specifications, Third Edition.
Array elements in Java are initialized to default values when created. For numbers this means they are initialized to 0, for references they are null and for booleans they are false.
To fill the array with something else you can use Arrays.fill() or as part of the declaration
int[] a = new int[] {0, 0, 0, 0};
There are no shortcuts in Java to fill arrays with arithmetic series as in some scripting languages.
int[] a = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
So I want to create this type of array. Note that this array has duplicate values. So I can't use hashMap's.
arr = [3,4,6,4,9,0]
I should also be able to do this:
arr[0].value = 9
arr[1].value = 10
Please help me with this type of data structure. It would be great if I can get one example of Java and one of Ruby.
Another easier to understand example.
arr['tom','jack', 'Ian','sam']
I wanna add a property of lastName to each array element. So my data structure so after adding this property.
arr['tom'].lastName = 'smith'
arr['jack'].lastName = 'parker'
arr['ian'].lastName = 'jones'
...
Now from a query perspective. if I call
arr[0] -> 'tom'
arr[0].lastName -> 'smith'
These are the results I am trying to retreive
Your intent is not really clear so I'll assume you want to replace values in your array. In ruby, you can do :
irb(main):001:0> a = [3, 4, 6, 4, 9, 0]
[]
irb(main):002:0> a[1]
4
irb(main):003:0> a[1] = 10
10
irb(main):004:0> a
[3, 10, 6, 4, 9, 0]
Say I have given array
int[] array = new int[50];
Then, say I assign 5 numbers to 5 locations
array[4] = 2
array[12] = 0
array[17] = 5
array[42] = 8
array[49] = 4
Is there a way I can loop through just the numbers I assigned without having a list that says "4, 12, 17, 42, 49" and get the output of "2, 0, 5, 8, 4"?
You can use the boxed type Integer which can be null.
Integer[] array = new Integer[50];
... assignment ...
for (Integer i : array)
if (i != null)
System.out.println(i);
Is there a way I can loop through just the numbers I assigned without having a list that says "4, 12, 17, 42, 49" and get the output of "2, 0, 5, 8, 4"?
No. you cannot do this with arrays. This will break the very purpose of array. You are actually expecting it to behave like a map.
Use proper map for this purpose. Here is an example
HashMap<Integer, Integer> map = new HashMap<>();
map.put(4, 2);
map.put(12, 0);
for(Integer value : map.values()) {
System.out.println(value);
}
Yes and no.
No, because you have to iterate over the whole array, unless you somehow store what values are set (what you don't want).
Yes, you can achieve to only get the output you want by setting some default values. For example initialize the array with Integer.MIN_VALUE and only handle values that are not equal to that value.
I know how to do it normally, but I could swear that you could fill out out like a[0] = {0,0,0,0}; How do you do it that way? I did try Google, but I didn't get anything helpful.
Check out the Arrays.fill methods.
int[] array = new int[4];
Arrays.fill(array, 1); // [1, 1, 1, 1]
You can also do it as part of the declaration:
int[] a = new int[] {0, 0, 0, 0};
Arrays.fill(). The method is overloaded for different data types, and there is even a variation that fills only a specified range of indices.
In Java-8 you can use IntStream to produce a stream of numbers that you want to repeat, and then convert it to array. This approach produces an expression suitable for use in an initializer:
int[] data = IntStream.generate(() -> value).limit(size).toArray();
Above, size and value are expressions that produce the number of items that you want tot repeat and the value being repeated.
Demo.
Arrays.fill(arrayName,value);
in java
int arrnum[] ={5,6,9,2,10};
for(int i=0;i<arrnum.length;i++){
System.out.println(arrnum[i]+" ");
}
Arrays.fill(arrnum,0);
for(int i=0;i<arrnum.length;i++){
System.out.println(arrnum[i]+" ");
}
Output
5 6 9 2 10
0 0 0 0 0
An array can be initialized by using the new Object {} syntax.
For example, an array of String can be declared by either:
String[] s = new String[] {"One", "Two", "Three"};
String[] s2 = {"One", "Two", "Three"};
Primitives can also be similarly initialized either by:
int[] i = new int[] {1, 2, 3};
int[] i2 = {1, 2, 3};
Or an array of some Object:
Point[] p = new Point[] {new Point(1, 1), new Point(2, 2)};
All the details about arrays in Java is written out in Chapter 10: Arrays in The Java Language Specifications, Third Edition.
Array elements in Java are initialized to default values when created. For numbers this means they are initialized to 0, for references they are null and for booleans they are false.
To fill the array with something else you can use Arrays.fill() or as part of the declaration
int[] a = new int[] {0, 0, 0, 0};
There are no shortcuts in Java to fill arrays with arithmetic series as in some scripting languages.
int[] a = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
Integer[] ints = list.toArray(new Integer[]{});
If I remove "{}" the compiler asks to fill in a dimension for the array. What do the two braces mean as a command?
It means you initialize the array with what is in between the braces. Ex:
new Integer[] { 1, 2, 3}
Makes an array with 1, 2 and 3. On the other hand:
new Integer[] {}
Just mean that you initialize an array without any values. So it is the same as new Integer[0].
This actually means empty array. The {} allow you to supply the elements of the array:
Integer[] ints = list.toArray(new Integer[]{1, 2, 3});
is equivalent to:
Integer[] ints = new Integer[3];
ints[0] = 1;
ints[1] = 2;
ints[2] = 3;
Check this link:
http://download.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
for more information - go to Creating, Initializing, and Accessing an Array section.
Yes, an empty array. Just like Integer[0].