Questions tagged [arraylist]

1

votes
2

answer
1.1k

Views

Sort Collection of GPS Locations by distance to current GPS Location

Given a collection of Location objects which have a longitude and latitude, how can I sort this list based on each Locations distance to a current Location object? I have a method which can calculate the distance in meters between two Location objects. Is Collections.Sort() appropriate here?
Ryan R
1

votes
3

answer
626

Views

Making ArrayList Size equal to other Array Lists Available in Java

I have 2 ArrayLists of varying sizes. Example - Array1.size() = 10 Array2.size() = 5 I want at all times these arrays to have same size. Thus, I have another class to ensure this. But obviously, it isn't working for me. Please help! Below is that Class code - for (int i = Array2.size(); i == Array1....
Bharat Nanwani
0

votes
1

answer
17

Views

PHP Reformat content array and Count same value

I have dynamically array for example like this: [{"Name":"Sutejo","status":"Overload"},{"Name":"Paiman","status":"Overload"},{"Name":"Agung Hercules","status":"Idle"},{"Name":"Heru","status":"Busy"}] How should i do if want to make my array become like this ? (count if status has same value and comb...
tonjel
1

votes
3

answer
370

Views

How to replace contents of arraylist with the contents of another arraylist?

I want to replace the contents of one arraylist with the contents of another completely. For instance, ArrayList old = new ArrayList(); ArrayList newlist = new ArrayList(); old.add("Hi"); old.add("World"); newlist.add("League") newlist.add("OfLegends"): old = newlist; When I try that, it does this...
Bob
1

votes
3

answer
5.5k

Views

inserting arraylist<String> values to sqlite in android

I have names and id's from listview in 2 different arraylist and would like to store id's and names from arraylist to string and then save them on a button click to sqlite. The problem with below code is that when 2 items are checked and button is clicked the items are inserting twice. And when the...
murtaza ali
0

votes
2

answer
36

Views

How can I turn this array into an ArrayList that does the same thing?

I want to make turn this program's array into an ArrayList. So far I know that the array will turn into ArrayList list = new ArrayList(); and that each list[i] will turn into: list.get(i) however I am not sure what the following line will be in order to satisfy the ArrayList version list[i] = new S...
cindy
1

votes
1

answer
140

Views

ArrayIndexOutOfBoundsException:-1

I'm coding some RL behaviors on a pacman bot and I messed things up with one of my lists in one of my functions arg_allmax or chooseAction Here is the code of my class: package rl; import java.util.ArrayList; import java.util.Hashtable; public class Qlearn { private double epsilon = 0.1; // Epsilon...
drheinrich940
1

votes
2

answer
55

Views

How to make a new arraylist with everything inside of it duplicated?

I am trying to create a method that takes an ArrayList and double everything inside of it. For example, if an ArrayList words stores ["hello", "there"] it should output ["Hello", "Hello", "there", "there"]. I already made a method that I think should work but it doesnt. Can anyone tell me where I go...
Niko
0

votes
0

answer
22

Views

How to write a working 2D Array List running sum function?

I have to be able to print the values of an array list in a matrix format with values increasing in the direction inputted in the method. For example, if 3=up I need a 2Dlist with the values 10 15 30 40;15 5 8 2;20 2 4 2;1 4 5 0 inputted to print: 46 26 47 44 36 11 17 4 21 6 9 2 1 4...
0

votes
0

answer
3

Views

how do i make it so that it takes the variable input and create a text file when it is done

i'm trying to make a program that lets you write something like a book or a todo list this i an idea i just had SET /P line1="" SET /P adlne=[add another line? Y or N?] IF "adlne"=="Y" GOTO :a IF "adlne"=="N" GOTO :b :a SET /P line2="" SET /P adlne=[add another line? Y or N?] IF "adlne"=="Y" GOTO...
kill griff
0

votes
0

answer
14

Views

Can not set java.util.List field to java.lang.String

I am getting below error Can not set java.util.List field com.packageName......ABCmode to java.lang.String com.packageName......ABCmode : I am not typing complete package name but the format is here to understand what I am doing. I get the error when below lines of code is being executed B1.hotRes...
Sam.jordan
1

votes
1

answer
20

Views

Android Robolectric: OutOfMemoryError - GC overhead limit exceeded, when Creating ArrayList

I want to create a test for a method that returns an ArrayList. The ArrayList type is a custom object called DateItem. But when I try to create the ArrayList in my test code (which is placed in test folder), the test failed with the following message: java.lang.OutOfMemoryError: GC overhead limit e...
Fawwaz Yusran
0

votes
10

answer
98

Views

How can I tell if the elements in an Array List are same or different?

I'm trying to verify if all the elements in an array list are same or not. This is my code: ArrayList arr = new ArrayList(Arrays.asList(2,2,4,2)); for (int z = 0; z < arr.size(); z++) { if(!arr.get(z++).equals(arr.get(z--))) { System.out.println("same"); }else { System.out.println("diff...
April Green
0

votes
0

answer
14

Views

Update excel file and if it doesn't exist create one? JAVA netbeans

I'm trying to gain more knowledge of Java, so I'm working on a project just for fun. Sadly, I have had a problem for the last few days and haven't been able to solve it yet, so I thought of trying out here. PROBLEM* I have this code below, and it works. I'm sure it's probably not the best way of ach...
0

votes
2

answer
33

Views

Add elements to a HashMap containing an ArrayList

This simple method is supposed to add elements to a HashMap. The elements are read from a txt file, I have tested that and everything regarding that area is working just fine. The issue comes when I print the values that are in the Map.Therefore, I think there might be some issue when adding them. T...
Luis Claramunt
-4

votes
0

answer
27

Views

How to compare sub lists inside an Array List

I am trying to compare array lists containing sub lists to verify whether they have same sub list elements or not. This is my code: ArrayList output = new ArrayList(); output.add("[objectives , video, activity]"); output.add("[objectives , video, activity]"); output.add("[objectives , jokes, activit...
April Green
0

votes
0

answer
6

Views

Desalinizing JSON Object Contains List of Objects Returns Null for the nested object

This is my first attempt of working with JSON deserialization. I have read many of the posts in Stackoverflow, non of the suggested solutions could match my issue, so my apology in advance. I have created the following objects: public class Item { public int ID { get; set; } public int LSum { get; s...
dolly_do
0

votes
2

answer
28

Views

Sorting an ArrayList<Object> works only with newer smartphones

i have to sort my ArrayList through a value of my Object. Its the distance value i calculate before. This works perfectly but if i try to sort it ascending, the older smartphones show different values than the actual distance is but the distance is calculated right. I think its because of my sort me...
Markus Gerlach
3

votes
3

answer
38

Views

Unite Collection of Collections into Collection in a method in Java

I've created a Collection class that extends ArrayList to add some useful methods. It looks like this: public class Collection extends ArrayList { //some methods... } I want to be able to unite a Collection of Collections to a single Collection, like this: {{1, 2}, {2,3}, {1}, {2}, {}} -> {1, 2, 2,...
Nick
0

votes
4

answer
35

Views

how to get the index for first occurence of any word from arraylist in sentence

I want to get the index of word from the sentence. But here I don't want to check for one specific word. I have list of words and I want to get index of the first occurrence of any word from the list which available in the sentence. I want the index to get the substring of the sentence, starting at...
PrajaktaParkhade
1

votes
2

answer
482

Views

Saving/loading multiple ArrayLists into one file in Java

Currently I have a program that can save and load ArrayLists of type "Variable" from a file. However, I would like to be able to save and load different objects(instances of different classes such as "Variables", "Functions"..etc) from just one file. Here is the code I have now: public SaveState(){...
Arman
0

votes
1

answer
36

Views

Adding to an ArrayList of objects? Java *SOLVED*

Just for starters this is for school. Not looking for an answer but I'm lost when adding both the date and weight into an object and then adding that object into an arraylist. public void addEntry(String d, int w) { Diet dietConstructor = new Diet(); entries = new Entry(d, w); diet.add(entries); } T...
m3m3th1n
1

votes
1

answer
3.8k

Views

How to make object of array list using jQuery

My project based on spring boot, Thymeleaf, mysql, html and jQuery. My Scenario is getting dynamically generated input box data and static input box data need to carried out and to hit the @controller. I want to POST as a JSON format in a type of List EntPropertySub that means list of class type, I...
Vignesh R
1

votes
3

answer
37

Views

Change the positions of list elements with some conditions

I have a list of strings like this :- listOf("abc", "a", "bb", "aa", "aaa", "bb", "a") I want an output like this :- listOf("a", "a", "aa", "bb", "bb", "aaa", "abc") First I want to sort list by length and then again sort that length group by letters. I tried below code so far fun main() { val resul...
i_m_mahii
1

votes
2

answer
63

Views

Sort arraylist by double and String

We were given a task to: sort an array list by a dog's tail length (double) if two dogs or more have the same tail length, sort them by name I was able to create a code BUT the correct results do not show up during the first print-out. I have to try again for the sorting to actually happen. I'm very...
M Ann
0

votes
2

answer
24

Views

finding in an array if specific fileds exists or not

This is my api {}JSON []products {}0 product-name : "Fiting" product-id : "1" product-description : "Door" product-image : "https://image/logo.jpg" product-categoryid : "2" category-name : "Furniture" {}1 product-name : "Bathroom" product-id : "2" product-description : "Services" product-image : "h...
Abhishek Kumar
0

votes
0

answer
15

Views

How can I use a java interface to calculate the union, intersection, and difference of two sets, all in a default method in the interface?

I am new to java so sorry if this is super basic. I need to make default methods in an interface ISet to allow for the calculation of the union, intersection, and difference of 2 sets created in a class that implements this interface. I know how to calculate all of these but I have no idea how to sy...
DawsonJBailey
0

votes
0

answer
27

Views

How to compare two HashMap if I have ArrayList?

I can not continue my HashMap homework. Can you help me? I have 2 HashMap: mtutors and mstudents. mtutors key: their name. mtutors value: a subject. mstudents key: their name. mstudents value: subjects(ArrayList) I have to find the matching tutor-students. For example: One of the mtutors key: Peter...
Dylan
-1

votes
0

answer
24

Views

program to shuffle 50 songs Same song should not be played twice ,Previous and next song if we click logic should be able to handle [on hold]

program to shuffle 50 songs  Given: • Play list has 50 songs  Expectation: • 50 songs will be played in random manner • Same song should not be played twice • Previous and next song if we click logic should be able to handle that • Once 50 songs are played it should sto...
Arun Prasad
0

votes
3

answer
28

Views

what is the most optimial way to convert Arraylist<Integer> to char[]

I am wondering is there anyways probably by stream API to convert List li = Arrays.asList(1,2,3,45,678); to char[] ch without going to an intermediate stage of String? I could not find one.
mukka
2

votes
3

answer
95

Views

Merge two strings in kotlin

I have two strings val a = "abc" val b = "xyz" I want to merge it and need output like below axbycz I added both strings to arraylist and then flatmap it val c = listOf(a, b) val d = c.flatMap { it.toList() } but not getting the desired result
Mahesh
0

votes
1

answer
23

Views

How to add element at a specific position in ArrayList while sorting using Arraylist?

I want to add an integer value at a specific position in an ArrayList but after doing so it pushes at last element to its next index, as i am doing sorting i do not want it to happen. Help me with a snippet of code to write bubble sort using ArrayList. My code void bubble_sorting(ArrayList arr){ int...
Saumya Pandey
0

votes
0

answer
13

Views

Why I am getting parse exception every time using following code?

I want to change the string into the date formate for that I am using SimpleDateFormate class. I am passing the string as "String+Integer.toString(int)" from list of strings and SimpleDateFormate patten as an inputs. Note: Instead of "String+Integer.toString(int)" if I pass actual string like "Jan 0...
Jigar Patel
14

votes
3

answer
848

Views

How to concatenate lists into one list

I have a list of values, some of which could be lists/collections or single values. In JavaScript notation it might look like: const input = [1,2,[3,4], [5,6], 7]; and I want to get: const concatenated = [1,2,3,4,5,6,7]; So I have this Java code: ArrayList concatenated = new ArrayList(); for (T v :...
MrCholo
1

votes
2

answer
2k

Views

Input an array of unknown size in java

How do I input an array whose length may vary? The input is space delimited and ends when I press enter public class ArrayInput { public static void main(String args[]){ ArrayList al = new ArrayList(); Scanner sc = new Scanner(System.in); while(){//what condition to use here? al.add(sc.nextInt()); }...
Tanvi
0

votes
0

answer
28

Views

Recursion method does not work to add element into arrayList

I am trying to create a recursion of arrayList (friends) to fill up another arrayList (recommendations). However, the recursion method does not work.The question is as below: How should we go about recommending new friends? We’ll take the approach of recommending all of our friends’ friends...
JaniceJ
-1

votes
4

answer
28

Views

get key and value from HashMap within ArrayList

I have a file that i get all the data and separate it into a HashMap. The file looks something like this below. Before the : is the key and after is the value key1: 1 key2: 2 key3: 3 this is the code that puts the file data into the map ArrayList: protected List yaml_parse(BufferedReader filename) t...
Steve Coulter
-1

votes
1

answer
32

Views

Adding vehicles to array list with constructor

I have created my class called AutoMobile(); I now need to create methods for an Auto Inventory Program to add a vehicle, print the list of vehicles, remove the vehicles, and update the attributes. What I am confused on, is how do I call the constructor to create a new car while accepting user in...
cobe
4

votes
3

answer
75

Views

Updating specific attribute of an ArrayList item

Item has name, price, condition attributes. I want to keep the priceand conditionbut replace the name. For now I figured out this by creating new object but I think this is not best option. I just want to change one field of ArrayList item public void modify(String name) { for (Item i : item) { if...
Sharkmen
1

votes
5

answer
40

Views

Return a min & max value and their positions within an arraylist using recursion

I need to write a recursive function that returns the largest and smallest elements in the ArrayList and the corresponding indices. To return these four items, I need to return an object that is an instance of an inner class called MinMaxObject that has four defined private variables: max, min, maxP...
Apollo_18

View additional questions