java ArrayList clone several times - java

I am trying to clone my ArrayList 4 times using a for-loop and clone() method, but I could not.
The below is the (straightforward) code I wrote:
static ArrayList<Integer> newArrList;
for (int n = 1; n <= 4; n++) {
ArrayList<Integer> arrList = new ArrayList<>();
for (int i = 1; i <= 13; i++) {
arrList.add(i);
}
newArrList = (ArrayList<Integer>) arrList.clone();
for (int i = 1; i <= 13; i++) {
newArrList.add(i);
}
}
The output is:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
It prints my arrList only 2 times, but I want it 4 times. Like that:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
Is it the best way to clone an ArrayList multiple times? and what am I doing wrong?

Your code seems a little too confusing to me, so I'm unable to point out where exactly you went wrong.
The first thing I don't understand is why you create your test list 4 times (inside the main loop). Shouldn't it be created just once?
Then doing a clone and adding the 13 elements afterwards put the content twice in the new list. Once as a copy of the original content, and then as hard-coded data. I don't understand the motivation behind that.
Anyway, I would just create a new (empty) list and add all the elements of the original list 4 times using addAll:
// create test list
ArrayList<Integer> arrList = new ArrayList<>();
for (int i = 1; i <= 13; i++) {
arrList.add(i);
}
// clone
ArrayList<Integer> newArrList = new ArrayList<>();
for (int i = 0; i < 4; i++) {
newArrList.addAll(arrList);
}

Related

Sum of all elements of each subarray

I was solving Kadane's algorithm , a very weird approach came to my mind while solving it. What if we find a way to find out the sum of all the elements of all possible subarrays forming from an array and store it in an arraylist. I've been thinking about it for a long time now, but I'm unable to solve it. It would be great if I can get some assistance.
`
import java.util.*;
import java.lang.*;
import java.io.*;
class Sample
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
for(int i=0;i<n;i++){
arr[i]=sc.nextInt();
}
ArrayList<Integer> list = new ArrayList<>();
int sum=0;
for(int i=0;i<n;i++){
sum+=arr[i];
}
list.add(sum);
for(int i=0;i<n;i++){
list.add(arr[i]);
}
for(int i=0;i<n;i++){
//sum-=arr[i];
if(!list.contains(sum-arr[i]) && (sum-arr[i])>0){
list.add(sum-arr[i]);
}
sum=sum-arr[i];
}
System.out.println(list);
}
}
`
This is what I've done till now. There's a very big flaw in the logic and I know it but I just can't seem to solve it.
This doesn't use Kadanes algorithm, but it does sum the sub arrays. It is facilitated by using the subList method of the List interface.
for (int i = 1; i < 8; i++) {
List<Integer> list =
IntStream.range(1, i+1).boxed().toList();
List<Integer> sums = new ArrayList<>();
findsubs(list, 0, list.size(), sums);
System.out.println(sums);
sums.clear();
}
prints
[1]
[3, 2, 1]
[6, 5, 3, 3, 2, 1]
[10, 9, 7, 4, 6, 5, 3, 3, 2, 1]
[15, 14, 12, 9, 5, 10, 9, 7, 4, 6, 5, 3, 3, 2, 1]
[21, 20, 18, 15, 11, 6, 15, 14, 12, 9, 5, 10, 9, 7, 4, 6, 5, 3, 3, 2, 1]
[28, 27, 25, 22, 18, 13, 7, 21, 20, 18, 15, 11, 6, 15, 14, 12, 9, 5, 10, 9, 7, 4, 6, 5, 3, 3, 2, 1]
The method
public static void findsubs(List<Integer> list, int s, int e,
List<Integer> sums) {
if (s >= e) {
return;
}
for (int i = s; i < e; i++) {
sums.add(list.subList(i, e).stream()
.mapToInt(Integer::intValue).sum());
}
findsubs(list, s, e - 1, sums);
}
long subarraySum(vector<long> &A) {
long result = 0;
int n = A.size();
for (int i=0; i <n; ++i)
{
result +=(A[i]*(i+1)*(n-i));
}
return result;
}
Sum of all sub array means,
Actually each element how many time contribute himself in each sub array
lets take : [1 2 3 4][0,0][0,1][1,1][0,2][1,2][2,2][0,3][1,3][2,3][3,3]
contribution of ith element is A[i](i+1)(n-i)

How can i display array to show integer?

I want to display something like this:
1 {1,2,3,4,5,6,7,8,9}
2 {1,2,3,4,5,6,7,8,9}
...
9 {1,2,3,4,5,6,7,8,9}
Nothing works
int[][] areas = new int[9][9];
for(int j=0; j<=8; j++)
{ // do i need to initialize here first array? something like areas [j][] = j ;
// how can i do that?
for(int i=0; i<=8; i++)
{
areas[j][i] = i;
Log.d("tiles", String.valueOf(areas[j][i])); // here i get from 1 to 9, 9 times
}
Log.d("area", String.valueOf(areas[j]));
//here is the problem
//i want to show 1 {for all tiles for this 1 area}, than 2 {for all tiles for
//this 2 area}... for each but it only display an address
}
I want to have control when I want to go to area 6 to tile 5 for example
Try Arrays.toString
int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
String s = Arrays.toString(arr).replace("[", "{").replace("]", "}");
for (int i = 1; i <= arr.length; i++) {
System.out.println(i + " " + s);
}
Ouput
1 {1, 2, 3, 4, 5, 6, 7, 8, 9}
2 {1, 2, 3, 4, 5, 6, 7, 8, 9}
3 {1, 2, 3, 4, 5, 6, 7, 8, 9}
4 {1, 2, 3, 4, 5, 6, 7, 8, 9}
5 {1, 2, 3, 4, 5, 6, 7, 8, 9}
6 {1, 2, 3, 4, 5, 6, 7, 8, 9}
7 {1, 2, 3, 4, 5, 6, 7, 8, 9}
8 {1, 2, 3, 4, 5, 6, 7, 8, 9}
9 {1, 2, 3, 4, 5, 6, 7, 8, 9}
int[][] areas = new int[10][10];
for (int j = 0; j <= 8; j++) {
System.out.print(j + 1);
System.out.print("{");
for (int i = 0; i <= 8; i++) {
areas[j][i] = i;
if (areas[j][i] < 8) {
System.out.print(String.valueOf(areas[j][i] + 1) + ","); // here i get from 1 to 9, 9 times
} else {
System.out.print(String.valueOf(areas[j][i] + 1));
}
}
System.out.print("}");
}
OUTPUT
1{1,2,3,4,5,6,7,8,9}2{1,2,3,4,5,6,7,8,9}3{1,2,3,4,5,6,7,8,9}4{1,2,3,4,5,6,7,8,9}5{1,2,3,4,5,6,7,8,9}6{1,2,3,4,5,6,7,8,9}7{1,2,3,4,5,6,7,8,9}8{1,2,3,4,5,6,7,8,9}9{1,2,3,4,5,6,7,8,9}

My selection sorting code is failing on one index

I'm having great issues with my selection sort code can anyone explain to me where i'm going wrong?
this code works for the most part however when it gets to the 4/5th round it false to sort the 5 as the next lowest instead going to the 6 in the array.
this is what my output looks like. As you can see the 5 is clearly in the wrong place.
any help understand why this is would be great thank you.
[1, 9, 4, 10, 5, 3, 6, 2, 8, 7]
[1, 2, 9, 10, 5, 3, 6, 4, 8, 7]
[1, 2, 3, 10, 9, 5, 6, 4, 8, 7]
[1, 2, 3, 4, 10, 9, 5, 6, 8, 7]
[1, 2, 3, 4, 6, 10, 9, 5, 8, 7]
[1, 2, 3, 4, 6, 5, 10, 9, 8, 7]
[1, 2, 3, 4, 6, 5, 7, 10, 9, 8]
[1, 2, 3, 4, 6, 5, 7, 8, 10, 9]
[1, 2, 3, 4, 6, 5, 7, 8, 9, 10]
-
public class Selection {
void findSmallestNumberIndex(int[] numbers, int index) {
//int[] numbers = {4, 9, 2, 10, 5, 3, 6, 1, 8, 7};
int n = numbers.length;
int min_idx = index; //4
for (int j = index + 1; j < n; j++) {
if (numbers[j] < numbers[min_idx]) {
min_idx = j;
}
int temp = numbers[min_idx];
numbers[min_idx] = numbers[index];
numbers[index] = temp;
}
}
public static void main(String[] args) {
int[] numbers = {4,9,2,10,5,3,6,1,8,7};
int NumLen = numbers.length;
int[] sortedNum = new int[NumLen];
int index;
index = 0;
Selection OB = new Selection();
do {
OB.findSmallestNumberIndex(numbers, index);
index++;
System.out.println(Arrays.toString(numbers));
} while (index != (NumLen - 1));
}
}
Move the logic to adjust array outside the for loop, here's how it should look:
void findSmallestNumberIndex(int[] numbers, int index) {
//int[] numbers = {4, 9, 2, 10, 5, 3, 6, 1, 8, 7};
int n = numbers.length;
int min_idx = index; //4
for (int j = index + 1; j < n; j++) {
if (numbers[j] < numbers[min_idx]) {
min_idx = j;
}
}
int temp = numbers[min_idx];
numbers[min_idx] = numbers[index];
numbers[index] = temp;
}

Large arraylist, finding the product of certain amount of elements

I have an array list which contains the numbers below, and what i am trying to do is find the product of every 16 numbers.
try {
for (int z = 0; z < 1000; z++) {
System.out.println(list.subList(z, z + 16));
the above prints this
[7, 3, 1, 6, 7, 1, 7, 6, 5, 3, 1, 3, 3, 0, 6, 2]
[3, 1, 6, 7, 1, 7, 6, 5, 3, 1, 3, 3, 0, 6, 2, 4]
[1, 6, 7, 1, 7, 6, 5, 3, 1, 3, 3, 0, 6, 2, 4, 9]
[6, 7, 1, 7, 6, 5, 3, 1, 3, 3, 0, 6, 2, 4, 9, 1]
etc......
my solution was to put every line above in to an array and find the product of that array.However i am stuck, was wondieering if anyone can provide me a few pointers on about how to take a hit at this
list.subList(z, z + 16);
for(int i = 0; i < list.subList(z, z+16).size();i++){
Ar[i] = list.get(z);
}
73167176531330624919225119674426574742355349194934
96983520312774506326239578318016984801869478851843
85861560789112949495459501737958331952853208805511
12540698747158523863050715693290963295227443043557
66896648950445244523161731856403098711121722383113
62229893423380308135336276614282806444486645238749
30358907296290491560440772390713810515859307960866
70172427121883998797908792274921901699720888093776
65727333001053367881220235421809751254540594752243
52584907711670556013604839586446706324415722155397
53697817977846174064955149290862569321978468622482
83972241375657056057490261407972968652414535100474
82166370484403199890008895243450658541227588666881
16427171479924442928230863465674813919123162824586
17866458359124566529476545682848912883142607690042
24219022671055626321111109370544217506941658960408
07198403850962455444362981230987879927244284909188
84580156166097919133875499200524063689912560717606
05886116467109405077541002256983155200055935729725
71636269561882670428252483600823257530420752963450
If you had a ArrayList of ArrayList then you could do
ArrayList <ArrayList> listofList = new ArrayList ();
for (int z = 0; z < 1000; z = z + 16) {
int endpoint = z + 16; // check to see not bigger than 1000
ArrayList thisList = list.subList(z, endpoint);
listOfList.add (thisList);
System.out.println(thisList);
}
But there again you may want to just add up as you go.
ArrayList thisList = list.subList(z, endpoint);
int prod = 1;
for (int x : thisList) {
prod *= x;
}
If you look at the printout that you are showing, you will that it is just moving one number each time - not what you want.
This is the solution I came up with. Instead of printing out the answer, you coudl add it to an array or something and use it later. Fyi I subbed the zeroes for 1's so I knew it was working.
public static void main(String[] args) throws Exception
{
int[] arr = {7, 3, 1, 6, 7, 1, 7, 6, 5, 3, 1, 3, 3, 1, 6, 2, 3, 1, 6, 7, 1, 7, 6, 5, 3, 1, 3, 3, 1, 6, 2, 4, 1, 6, 7, 1, 7, 6, 5, 3, 1, 3, 3, 1, 6, 2, 4, 9};
int counter = 0;
int product = 1;
for (int i : arr)
{
if (counter < 16)
{
product *= i;
counter++;
}
if (counter >= 16)
{
System.out.println(product);
product = 1;
counter = 0;
}
}
}
This is what printed (again I subbed the zeroes for ones):
60011280
34292160
102876480

Rearranging an array in Java

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.

Categories