In the following code, I am wondering how to use the other object. Specifically how do I get the two ArrayLists to have different values within them when I am passing them into the append method.
The following code is supposed to append two Arraylists together without modifying either of them.
I understand that in order to do this, I need to create a separate instance of the Arraylist "values", thats why I used the other object, but I am wondering how do I assign separate values to each instance of the Arraylist
package com.company;
import java.util.ArrayList;
public class MergeSequence {
public ArrayList<Integer> values;
public MergeSequence(){
values = new ArrayList<Integer>();
}
public void add(int n) {
values.add(n);
}
public String toString() {
return values.toString();
}
public MergeSequence append(MergeSequence other)
{
MergeSequence result = new MergeSequence(); // Create a new result object.
// Iterate through the "local" ArrayList and add each value to the result
for (int i = 0; i < values.size(); i++)
{
int j = values.get(i);
result.add(j);
}
// Now, iterate through the "external" ArrayList and add each value to the result
for (int i = 0; i < other.values.size(); i++)
{
int j = other.values.get(i);
result.add(j);
}
result.toString();
// Then return the result. Neither source ArrayList is modified.
return result;
}
}
toString itself doesn't output anything to std-out. So after calling result.toString(); you won't see the result in the console. You need to use System.out.println(result) to output the result.
By the way, printing the values in the append method is a side effect. It would be better to print the values outside of the append. E.g.:
public static void main(String[] args) {
final MergeSequence a1 = new MergeSequence();
a1.add(1);
a1.add(2);
final MergeSequence a2 = new MergeSequence();
a2.add(3);
final MergeSequence a3 = a1.append(a2);
System.out.println(a1); // [1, 2]
System.out.println(a2); // [3]
System.out.println(a3); // [1, 2, 3]
}
Just add a main method to test
public static void main(String[] args) {
MergeSequence m1 = new MergeSequence();
m1.add(1);
m1.add(2);
MergeSequence m2 = new MergeSequence();
m2.add(3);
MergeSequence m3 = m1.append(m2);
System.out.println(m3.toString());
}
Related
In this program I've created a class called ArrayExample and created a random number to fill its elements and a toString() method with it. Why am I getting null as my output? The code has no errors from what it seems like. :
public class arran {
public static void main(String[] args) {
ArrayExample[] no1 = new ArrayExample[5];
System.out.println(Arrays.toString(no1));
}
public class ArrayExample {
int[] array;
ArrayExample(int len) {
array = new int[len];
for (int i = 0; i < array.length; i++) {
array[i] = (int) (Math.random() * 100);
}
}
#Override
public String toString() {
StringBuilder sb = new StringBuilder(array.length);
for (int i = 0; i < array.length; i++) {
sb.append(array[i]);
}
return sb.toString();
}
}
}
You're creating ArrayExample[5], which is an array of five ArrayExample objects, and not a single ArrayExample with a constructor argument of 5. It looks like your intent was to invoke the constructor ArrayExample(int len), but it's never called.
Try this, instead:
public static void main(String[] args) {
ArrayExample no1 = new ArrayExample(5); // Invokes the constructor you wrote
System.out.println(no1.toString()); // Uses the toString method your wrote
}
Make your inner class static so it can be accessed by your main method:
public static class ArrayExample {
And fix your .toString() method:
public String toString() {
return Arrays.toString(array);
}
The problem is in this code:
ArrayExample[] no1 = new ArrayExample[5];
System.out.println(Arrays.toString(no1));
This declares an array of a certain type (ArrayExample), and a certain length (5), but it doesn't do anything else. That first line results in a variable reference no1 containing five null values. That's what you see when you print it out.
[null, null, null, null, null]
Here's another example, but using Object to highlight that the code isn't doing anything other than creating an empty array and printing the null values.
Object[] no2 = new Object[5];
System.out.println(Arrays.toString(no2));
This code has the same output:
[null, null, null, null, null]
Here is code that will initialize the array (like you already were doing), then create a new instance of ArrayExample and save it at each array element. (I also moved ArrayExample out of the main class definition so that it compiles).
public class Example {
public static void main(String[] args) {
ArrayExample[] no1 = new ArrayExample[5];
no1[0] = new ArrayExample(10);
no1[1] = new ArrayExample(10);
no1[2] = new ArrayExample(10);
no1[3] = new ArrayExample(10);
no1[4] = new ArrayExample(10);
System.out.println(Arrays.toString(no1));
}
}
class ArrayExample {
// use same class contents as your original post
}
Here is output from running this updated code:
[6474373150570442153, 27524825597078735826, 58127219204026904839, 90611080549351937198, 6503557163540938849]
Another option for setting your array contents would be using this one-liner:
Arrays.setAll(no1, k -> new ArrayExample(10));
I am trying to sum N pairs of ints--an Nx2 ArrayList--and return the N summations as an ArrayList. While I understand it is not necessary to set up a class to accomplish this, I would like to do so as practice for future projects.
import java.util.ArrayList;
public class SumsInLoop {
public SumsInLoop(int numberOfPairs, ArrayList<ArrayList<Integer>> numbersList) {}
public ArrayList<Integer> getSums(int numberOfPairs, ArrayList<ArrayList<Integer>> numbersList) {
ArrayList<Integer> pairsOfSums = new ArrayList<Integer>();
for (ArrayList<Integer> Pair : numbersList) {
int x = Pair.get(0);
int y = Pair.get(1);
int sum = x + y;
pairsOfSums.add(sum);
}
System.out.println(pairsOfSums);
return pairsOfSums;
}
The data that I am given is a random assortment of N pairs (numbersOfPairs) of integers, e.g. 612673 108695. I would like to add these pairs of integers to a 2D ArrayList (numbersList) that will be called by getSums.
However, I am having difficulties initializing numbersList. My main function is as follows:
public static void main(String[] args) {
int myNumberOfPairs = 13;
ArrayList[][] myNumbersList = new ArrayList[13][2];
myNumbersList[0][0] = new ArrayList<>();
myNumbersList[0][0].add(612673);
myNumbersList[0][1].add(108695);
myNumbersList[1][0] = new ArrayList<>();
myNumbersList[1][0].add(756875);
myNumbersList[1][1].add(496058);
SumsInLoop mySum = new SumsInLoop(myNumberOfPairs,myNumbersList);
mySum.getSums(myNumberOfPairs, myNumbersList);
The last two lines of code throw errors, asking me to change myNumbersList to type ArrayList<List<Integer>> which throws even more errors, even after changing all 2D ArrayLists to type ArrayList<List<Integer>>.
So, my two questions are as follows:
How can I initialize an NxM ArrayList and populate it correctly?
Is there a faster way of accomplishing this task while still using a class method?
P.S. I'm used to coding in Python and am self-teaching myself Java, so any other information or resources you can provide me with are much appreciated.
You may want to simplify your input by using 2D array of int : int[][] myNumbersList = new int[13][2];
The expected output in that case is a 1D array of int[13] that can be obtained as follows (demonstrated with 2 pairs. See mcve ) :
public class SumsInLoop {
//pairsOfInts should be an [n][2] array
private static int[] sumOfPairs(int[][] pairsOfInts) {
int[] sums = new int[pairsOfInts.length];
for(int pairIndex = 0; pairIndex < pairsOfInts.length; pairIndex++) {
sums[pairIndex]= pairsOfInts[pairIndex][0]+pairsOfInts[pairIndex][1];
}
return sums;
}
public static void main(String[] args) {
int numberOfPairs = 2;
int[][] pairsOfInts = new int[numberOfPairs][2];
pairsOfInts[0] = new int[] {612673,108695 };
pairsOfInts[1] = new int[] {756875,496058 };
int[] sumOfPairs = sumOfPairs(pairsOfInts);
System.out.println(Arrays.toString(sumOfPairs));
}
}
If you want a solution implemented with List you can make use of javafx Pair (or make your own pair class.
The input can be defined as List<Pair<Integer,Integer>> pairsOfInts = new ArrayList<>();
The out put can be an array as above, or a List<Integer>:
import java.util.ArrayList;
import java.util.List;
import javafx.util.Pair;
public class SumsInLoop {
private static List<Integer> sumOfPairs(List<Pair<Integer, Integer>> pairsOfInts) {
List<Integer> sums = new ArrayList<>();
for(Pair<Integer,Integer> pair : pairsOfInts) {
sums.add(pair.getKey()+ pair.getValue());
}
return sums;
}
public static void main(String[] args) {
List<Pair<Integer,Integer>> pairsOfInts = new ArrayList<>();
pairsOfInts.add (new Pair<>(612673,108695 ));
pairsOfInts.add (new Pair<>(756875,496058));
List<Integer> sumOfPairs = sumOfPairs(pairsOfInts);
System.out.println(sumOfPairs);
}
}
The (compile) exception you are getting is due to the fact that you expect a ArrayList<ArrayList<Integer>>, but pass an ArrayList[][]. (which is not the same in Java)
In your case you'd need (in the main method):
ArrayList<ArrayList<Integer>> myNumbersList = new ArrayList</* when java > 6 ;)*/>(13);
this only sets the capacity of the (parent) list (..and the underlying/internal backing array)
to initialize the child lists, you'd not come around looping (somehow...even not in python :):
for (int i = 0; i < 13; i++) {
myNumbersList.add(new ArrayList<Integer>(2));
}
Depends on what means "correctly" ...but I assume with "random data", ideally you would again inner loop:
java.util.Random rnd = new Random(/*seed default current timestamp*/);
//...
for (int i = 0; i < 13; i++) {
ArrayList<Integer> innerList = new ArrayList<>(2);
for (int j = 0; j < 2; j++) {
innerList.add(rnd.netxInt(/*bound default Integer.MAX_VALUE*/) /*+/-/% offset*/);
}
myNumberList.add(innerList);
}
Sorry I am not aware of one (faster way), but much depends on the "input format".
Since you already know the amount of values in a pair, an ArrayList is unnecessary. You can create your own, simpler implementation of a pair.
class Pair {
public final int left;
public final int right;
public Pair(int left, int right){
this.left = left;
this.right = right;
}
}
You can then access the values by creating a pair object and accessing its fields.
Pair p = new Pair(10, 7);
System.out.println(p.left); // 10
System.out.println(p.right); // 7
You can then more easily redefine your getSums method.
public static List<Integer> getSums(List<Pair> pairs){
List<Integer> pairsOfSums = new ArrayList<>();
for(Pair pair : pairs){
int sum = pair.left + pair.right;
pairsOfSums.add(sum);
}
return pairsOfSums;
}
Please also notice the function can be static and you don't need to pass the number of pairs. The for-each loop will cycle through all of them regardless.
Initializing the array is then easier than the method you have described in the question.
List<Pair> pairs = new ArrayList<>();
pairs.add(new Pair(7, 10));
pairs.add(new Pair(18, 3));
pairs.add(new Pair(-6, 0));
pairs.add(new Pair(4, 2));
System.out.println(SumsInLoop.getSums(pairs));
I have an arraylist that looks like this:
public static ArrayList<ArrayList<String[]>> x = new ArrayList<>();
I store groups of 2 persons in a pair. For example:
[Person1, Person2]
[Person3, Person4]
The algorithm I use right now still makes duplicates, I've tried out hashmaps and iterating through them with for loop but they just give me back the original list.
This is the code:
package com.company;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;
public class createGroups
{
public static ArrayList<ArrayList<String[]>> x = new ArrayList<>();
public static void main(String[] args){
//Define names
String[] names = {"Person1", "Person2", "Person3", "Person4"};
try
{
//Create combinations. In a try catch because of the saveFile method.
combination(names, 0, 2);
//Print all the pairs in the Arraylist x
printPairs();
} catch (IOException e)
{
e.printStackTrace();
}
}
static void combination(String[] data, int offset, int group_size) throws IOException
{
if(offset >= data.length)
{
//Create new Arraylist called foo
ArrayList<String[]> foo = new ArrayList<>();
//Create a pair of 2 (data.length = 4 / group_size = 2)
for(int i = 0; i < data.length / group_size; i++)
{
//Add the pair to foo.
foo.add(Arrays.copyOfRange(data, 2 * i, 2 * (i + 1)));
}
//Add foo to x
x.add(foo);
//saveFile(foo);
}
for(int i = offset; i < data.length; i++){
for(int j = i + 1; j < data.length; j++){
swap(data, offset, i);
swap(data, offset + 1, j);
combination(data, offset + group_size, group_size);
swap(data, offset + 1, j);
swap(data, offset, i);
}
}
}
public static void printPairs(){
//Print all pairs
for(ArrayList<String[]> q : x){
for(String[] s : q){
System.out.println(Arrays.toString(s));
}
System.out.println("\n");
}
}
private static void swap(String[] data, int a, int b){
//swap the data around.
String t = data[a];
data[a] = data[b];
data[b] = t;
}
}
The output right now is this:
Output
Every group of 4 names is a 'list' of pairs (Not really a list but that's what I call it)
And this is the desired output:
Desired output
But then you can see that the first and the last list of pairs are basically the same how do I change that in my combination method
The question:
How can I change my combination method so that it doesn't create duplicate groups.
And how can I make the list smaller (The desired output) when printing the created lists.
If I wasn't clear enough or if I didn't explain what I want very well, let me know. I'll try to make it clearer.
Create an object similar to this. It takes 4 strings (2 pairs). Puts the strings into array and sorts this array. That means any combination of strings you put in will be converted into one sorted combination, but the object internaly remembers which person is person1, person2, ...
private class TwoPairs {
private final String person1;
private final String person2;
private final String person3;
private final String person4;
private final String[] persons;
TwoPairs(String person1, String person2, String person3, String person4) {
this.person1 = person1;
this.person2 = person2;
this.person3 = person3;
this.person4 = person4;
persons = new String[4];
persons[0] = person1;
persons[1] = person2;
persons[2] = person3;
persons[3] = person4;
// if we sort array of persons it will convert
// any input combination into single (sorted) combination
Arrays.sort(persons); // sort on 4 objects should be fast
// hashCode and equals will be comparing this sorted array
// and ignore the actual order of inputs
}
// compute hashcode from sorted array
#Override
public int hashCode() {
return Arrays.hashCode(persons);
}
// objects with equal persons arrays are considered equal
#Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null) return false;
if (getClass() != obj.getClass()) return false;
TwoPairs other = (TwoPairs) obj;
if (!Arrays.equals(persons, other.persons)) return false;
return true;
}
// add methods which you might need
// getters for individual persons
// String getPerson1() { return person1; }
// or perhaps pairs of persons
// String[] getPair1() { return new String[] {person1, person2}; }
// add sensible toString method if you need it
}
Your ArrayList x will change like this
ArrayList<TwoPairs> x = new ArrayList<TwoPairs>();
before adding new TwoPairs object into x check if this list already contains this object.
if (!x.contains(twoPairsObject)) {
x.add(twoPairsObject);
}
I have created a class using linked list to display 20 Fibonacci numbers. Here is my code:
import java.util.LinkedList;
public class FibonacciLinkList {
private LinkedList<Integer> fibonacciList;
public FibonacciLinkList(LinkedList<Integer> FibonacciLinkList) {
this.fibonacciList = FibonacciLinkList;
}
public LinkedList<Integer> sum()
{
int n, a = 0, b = 0, c = 1;
for(int i = 1; i <= 20; i++)
{
a = b;
b = c;
c = a + b;
}
return fibonacciList;
}
public void display() {
System.out.println(fibonacciList);
}
public static void main(String[] args) {
LinkedList fibonacciList = new LinkedList();
fibonacciList.display(); //This is where the error is
}
}
The problem I am having is displaying the Fibonacci numbers on the console.
I have tried to do this by using a display method but it hasn't really worked for me. I have done a lot of searching online and on SO and have tried them but they have not worked for me. It would be appreciated if you could fix my code so that it does work.
I am new to linked list and this is the first time I am coding a linked list myself and I feel that a solution to this problem will help me understand linked lists better.
As I mentioned, LinkedList is not an instance of FibonacciLinkedList, and it does not possess the display() method. Attempting to invoke it on the LinkedList object will lead to failure to compile.
The sum() method is not invoked nor does it actually do anything. That is, it does not assign anything to the fibonacciList you have.
I would recommend that you extend the LinkedList class and generate the items on instantiation. Then, using the default toString() you can display to console. After all, the class is simply an extension of the LinkedList data structure to store Fibonacci numbers up to 20.
As you extend LinkedList, you inherit the AbstractCollection.toString() method for which the "string representation consists of a list of the collection's elements in the order they are returned by its iterator, enclosed in square brackets ("[]")."
public class FibonacciLinkedList extends LinkedList<Integer> {
public FibonacciLinkedList(int n){
int a = 0, b = 0, c = 1;
for(int i = 1; i <= n; i++) {
a = b;
b = c;
c = a + b;
this.add(c);
}
}
public void display() {
System.out.println(this.toString());
}
public static void main(String[] args) {
FibonacciLinkedList list = new FibonacciLinkedList(20);
list.display();
}
}
I fixed your code:
import java.util.LinkedList;
public class FibonacciLinkList {
private LinkedList<Integer> fibonacciList;
public FibonacciLinkList() {
this.fibonacciList = new LinkedList<Integer>();
}
public LinkedList<Integer> sum()
{
int n, a = 0, b = 0, c = 1;
for(int i = 1; i <= 20; i++)
{
fibonacciList.add(a);
a = b;
b = c;
c = a + b;
}
return fibonacciList;
}
public void display() {
System.out.println(fibonacciList);
}
public static void main(String[] args) {
FibonacciLinkList fibonacciList = new FibonacciLinkList();
fibonacciList.sum();
fibonacciList.display();
}
}
Try this.
There is several points that you need to take care :
sum() is never called.
the look in sum() does not change fibonacciList, it only uses local variables and does nothing else with it.
display() is NOT a LinkedList function, so it will likely not work. And even if it were working, it will likely not display what you expect : you need to loop through the list and print each value.
an other fibonacciList is created in the main function, so the display (if it was working) would show the content of this local list and not the global one.
I am adding five objects in the list with the help of for loop. I am initializing my object outside the for loop. In the body of for loop i am changing the object setter properties and adding it in the list. The output with this is: It will add five objects but all have the same attributes even after setting the different values for the attribute.
See the following code
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class ASD {
public static void main(String args[]) {
List list = new ArrayList<A>();
System.out.println("Before Insert List is " + list);
A obj = new A();
for (int i = 0; i < 5; i++) {
obj.setA(new Random().nextInt(10));
list.add(obj);
}
System.out.println("After Insert List is " + list);
for (int i = 0; i < 5; i++) {
A prObj = (A) list.get(i);
System.out.println("Values are" + prObj.getA());
}
}
}
class A {
int a;
public int getA() {
return a;
}
public void setA(int a) {
this.a = a;
}
}
If I initialize A's object inside the for loop then it will add five objects and also changes the attribute for the objects. Can anyone explain this behaviour
You have created just one instance and setting it several times inside the for loop.Create a new instance of A inside the for loop, not outside it
for (int i=0;i<5;i++) {
A obj = new A();
obj.setA(new Random().nextInt(10));
list.add(obj);
}
When you are doing this -
A obj = new A();
for (int i=0;i<5;i++) {
obj.setA(new Random().nextInt(10));
list.add(obj);
you are actually adding reference to the same object after changing it's attribute setA.
That way, all of the list elements have reference to the same object (with same value of a).
You need to add new objects if you want to have different values -
for (int i=0;i<5;i++) {
obj = new A(); // new object
obj.setA(new Random().nextInt(10));
list.add(obj);