How can I store arrays in single array?
e.g. I have four different arrays, I want to store it in single array int storeAllArray [] and when I call e.g. storeAllArray[1] , I will get this output [11,65,4,3,2,9,7]instead of single elements?
int array1 [] = {1,2,3,4,5,100,200,400};
int array2 [] = {2,6,5,7,2,5,10};
int array3 [] = {11,65,4,3,2,9,7};
int array4 [] = {111,33,22,55,77};
int storeAllArray [] = {array1,array2,array3,array2} // I want store all array in on array
for (int i=0; i<storeAllArray; i++){
System.out.println(storeAllArray.get[0]); // e.g. will produce --> 1,2,3,4,5,100,200,400 , how can I do this?
}
EDITED:
How can I get output like this?
System.out.println(storeAllArray [0]) --> [1,2,3,4,5,100,200,400];
System.out.println(storeAllArray [1]) --> [2,6,5,7,2,5,10];
System.out.println(storeAllArray [2]) --> [11,65,4,3,2,9,7];
System.out.println(storeAllArray [2]) --> [111,33,22,55,77];
int array1 [] = {1,2,3,4,5,100,200,400};
int array2 [] = {2,6,5,7,2,5,10};
int array3 [] = {11,65,4,3,2,9,7};
int array4 [] = {111,33,22,55,77};
int[] storeAllArray [] = {array1,array2,array3,array4};
for (int[] array : storeAllArray) {
System.out.println(Arrays.toString(array));
}
In Java 5 and above, this prints
[1, 2, 3, 4, 5, 100, 200, 400]
[2, 6, 5, 7, 2, 5, 10]
[11, 65, 4, 3, 2, 9, 7]
[111, 33, 22, 55, 77]
Prior to Java 5, you should use
System.out.println(Arrays.asList(array));
If I understand your question, you want to "flatten" those arrays to one array. Look at rosettacode.org for such example in Java and other languages.
int array1[] = { 1, 2, 3, 4, 5, 100, 200, 400 };
int array2[] = { 2, 6, 5, 7, 2, 5, 10 };
int array3[] = { 11, 65, 4, 3, 2, 9, 7 };
int array4[] = { 111, 33, 22, 55, 77 };
int[][] storeAllArray = new int[][] { array1, array2, array3, array2 };
for (int j : storeAllArray[0]) {
System.out.print(j + ", ");
}
To access a single element of the selected array you need to do something like:
storeAllArray[i][j]
use the following syntax
int[][] storeAllArray = {array1, array2, array3, array4};
Create array-of-arrays:
int[] array1 = {1,2,3,4,5,100,200,400};
int[] array2 = {2,6,5,7,2,5,10};
int[] array3 = {11,65,4,3,2,9,7};
int[] array4 = {111,33,22,55,77};
int[][] allArrays = {
array1, array2, array3, array4
};
System.out.println(java.util.Arrays.toString(allArrays[0]));
// prints "[1, 2, 3, 4, 5, 100, 200, 400]"
There is no need to do so.
You can use a two dimensional array for the job.
Make sure the row length should be equal to the array with max length.
int a[]={1,2,3,4,5,6};
int b[]={4,8,3,6,4,5};
int c[][]=new int[2][6];//here 2 refers to the no. of arrays and 6 refers to number of elements in each array
Related
public class Main {
public static void main(String[] args) {
int[][] numbers = {{1, 2, 3} , {4, 5, 6, 7}}; //Is it defined?
int x = numbers[0][3]; // Should the output be 0?
System.out.println(x);
}
}
you may take a look here into the official oracle specification:
https://docs.oracle.com/javase/specs/jls/se7/html/jls-10.html
When you take a look on chapter (array access) you will see that:
"All arrays are 0-origin. An array with length n can be indexed by the integers 0 to n-1."
A 2D array is really an array of arrays. So you can do this.
int[][] arr = new int[4][];
arr[0] = new int[] {1,2,3};
arr[1] = new int[] {4,5,6,7};
arr[2] = new int[] {8,9,10,11,12,13};
arr[3] = new int[] {14,15};
And to print them.
for (int[] a : arr) {
System.out.println(Arrays.toString(a));
}
Prints
[1, 2, 3]
[4, 5, 6, 7]
[8, 9, 10, 11, 12, 13]
[14, 15]
Technically, there are no two-dimensional arrays. What you can have is an array of arrays; this is conveyed precisely by the [][] syntax.
In your example,
int[][] numbers = {{1, 2, 3} , {4, 5, 6, 7}};
the 0'th element of numbers, i.e., numbers[0], is a 3-element array, whereas numbers[1], is a 4-element array.
Accessing numbers[0][3] will raise an exception, since 3 is out of bounds for the 3-element array.
Reference to Java Language Specification: Chapter 10, Arrays.
How do I insert values in array whose size may change and I should get the values dynamically and I should do the same with another array.
The size of the array may range From 1 to 100. I should compare the values in both the arrays and display the odd man out.
Example
With lists x = [13, 5, 6, 2, 5] and y = [5, 2, 5, 13], the function answer(x, y) would return 6 because the list x contains the integer 6 and the list y doesn't.
Given the lists x = [14, 27, 1, 4, 2, 50, 3, 1] and y = [2, 4, -4, 3, 1, 1, 14, 27, 50], the function answer(x, y) would return -4 because the list y contains the integer -4 and the list x doesn't
class ArrayLi
{
public static void main(String[] args)
throws IOException
{
int n=99,ins,i,j;
ArrayList<Integer> x = new ArrayList<Integer>(n);
ArrayList<Integer> y = new ArrayList<Integer>(n)
// Appending the new element at the end of the list
//for(int var : arrli)
for (i=1;i<n; i++)
x.add(i);
// Printing elements
System.out.println(x);
// Remove element at index 3
for (j=1;j<n; j++)
y.add(j);
// Displaying ArrayList after deletion
System.out.println(y);
// Printing elements one by one
for (int i=0; i<x.size(); i++)
System.out.print(x.get(i)+" ");
for(int i=1;i<x.length;i++){
for(int j=1;j<y.length;j++){
if(x[i]==y[j]){
System.out.println(x[i]);
}
}
Unless you really need to use arrays and/or do everything manually there is an easy way using Java 8's new API:
private static List<Integer> answer(List<Integer> x, List<Integer> y) {
return Stream.concat(x.stream(), y.stream())
.filter(i -> !x.contains(i) || !y.contains(i))
.collect(Collectors.toList());
}
Sample usage:
public static void main(String[] args) {
List<Integer> x1 = Arrays.asList(13, 5, 6, 2, 5);
List<Integer> y1 = Arrays.asList(5, 2, 5, 13);
List<Integer> x2 = Arrays.asList(14, 27, 1, 4, 2, 50, 3, 1);
List<Integer> y2 = Arrays.asList(2, 4, -4, 3, 1, 1, 14, 27, 50);
System.out.println(answer(x1, y1));
System.out.println(answer(x2, y2));
}
Prints:
[6]
[-4]
A pretty easy way would be to turn your arrays into Lists.
You could do the following:
List<Integer> xList = Arrays.asList(x);
List<Integer> yList = Arrays.asList(y);
Then, if the original x array is larger, do xSet.removeAll(ySet), or vice versa ySet.removeAll(xSet).
I have an array of 3 elements arraylist = {10000, 7000, 2000}.
I have 3 more elements {1,2,5,7}, {1,3,4,5,7} and {1,3,4,6,7}, where each of them are also arrays and I will get each of them from a loop. Each time the loop executes I will get an array like [1,2,5,7].
Now I want to execute each array {1,2,5,7}, {1,3,4,5,7} or {1,3,4,6,7} for only one value of array.
For example:
I want to execute
[1,2,5,7] for the value 10000,
[1,3,4,5,7] for 7000
and [1,3,4,6,7] for 2000.
I have tried to use for each loop but it does not fullfill my requirement.
Can anyone give me any solution or hint how to solve this problem??
int array[] = {10000, 7000, 2000};
int paths[][] = {
{1, 2, 5, 7},
{1, 3, 4, 5, 7},
{1, 3, 4, 6, 7}
};
for(int i = 0; i < array.length; i++){
int arrayElm = array[i]; // Here you will get array Element e.g. 10000, 2000 etc..
int pathElm[] = paths[i]; // Here you will get a path array e.g. [1, 2, 5, 7] etc..
// Do your operation here...
}
Code should look like this, it will read and print on console.
tempPath should be an array.
for(int i=0;i<array.length;i++)
{
System.out.println(array[i]) // Prints element of array.
tempPath = paths[i];
for(int j=0;j<tempPath.length;j++)
{
System.out.println(tempPath[j]); //prints individual elements from Path Array
}
}
Based on your example, you want some map, where arraylist contain keys, and paths is values.
For example you can use something like that:
List array = Arrays.asList(10000, 7000, 2000);
int paths[][] = {
{1, 2, 5, 7},
{1, 3, 4, 5, 7},
{1, 3, 4, 6, 7}
};
int requiredKey = 7000;
int[] result = paths[array.indexOf(requiredKey)];
but better just keep all this data into some Map
In my app I have 2 arrays
Integer[] array1= {1, 2, 3, 8, 6, 4, 10};
Integer[] array2= {200, 100, 50, 20, 10, 5, 1};
Now I want to multiply every element from the first array with every element from the second array and finally to sum, for example:
200x1 = 200; 100x2 = 200; 50x3 = 150; 20x8 = 160; 10x6 = 60; 5x4 = 20; 1x10 = 10;
and then to sum 200+200+150+160+60+20+10 = 800
You can even use streams:
Integer[] array1 = {1, 2, 3, 8, 6, 4, 10};
Integer[] array2 = {200, 100, 50, 20, 10, 5, 1};
public void test() {
System.out.println(
// Each index of the arrays.
IntStream.range(0, Math.min(array1.length, array2.length))
// Multiply the two array entries.
.mapToLong(n -> array1[n] * array2[n])
// Add them up.
.sum());
}
Integer[] array1= {1, 2, 3, 8, 6, 4, 10};
Integer[] array2= {200, 100, 50, 20, 10, 5, 1};
int resultOfSum = 0;
for (int i = 0; i < array1.length;i++){
resultOfSum += array1[i]*array2[i];
}
Hope this helps :)
use this
int total = 0;
for(int i = 0; i < array1.length; i++){
total += array2[i] * array1[i];
}
Here, try this :
int s=0;
for(int i=0;i<array1.length;i++)
s+=array1[i]*array2[i];
System.out.println(s);
If I have an array:
int[] myArray = new int[10]
for (int i = 0; i < myArray.length; i++) {
myArray[i] = i;
}
//resulting array: {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
How can I move everything behing the 4 up one space, and send the 4 to the back? Example:
this:
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
into this:
{0, 1, 2, 3, 5, 6, 7, 8, 9, 4}
How about this:
int[] myArray = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
System.arraycopy(myArray, 5, myArray, 4, 5);
myArray[myArray.length-1] = 4;
In the above code, I'm using the arraycopy method to copy a range of 5 numbers starting from index 5, to index 4 in the array, and then simply set a 4 in the last position.
Notice that using arraycopy is much faster than copying the values in a loop, since it's usually implemented as a native operation which copies memory positions.
EDIT :
A more generic solution, a method for sending to the back a given position in the array:
public static void sendBack(int[] array, int idx) {
int value = array[idx];
System.arraycopy(array, idx+1, array, idx, array.length-idx-1);
array[array.length-1] = value;
}
For your example, call it like this:
sendBack(myArray, 4);
// now myArray is {0, 1, 2, 3, 5, 6, 7, 8, 9, 4}
Like this?
int start = 4;
int temp = myArray[start];
for(int i = start; i < myArray.length - 1; i++) {
myArray[i] = myArray[i+1];
}
myArray[myArray.length-1] = temp;
It's the fastest way I can guess...
Use System.arraycopy.