[Solved] What are the differences between ArrayList and ArrayMap?

ArrayMap keeps its mappings in an array data structure — an integer array of hash codes for each item, and an Object array of the key -> value pairs. Where ArrayList is a List. Resizable-array implementation of the List interface. Implements all optional list operations, and permits all elements, including null. FYI ArrayMap is a … Read more

[Solved] Default constructor issue when creating an array Java

The error message tells you exactly what’s wrong. There is no declared variable by the name of array. public class ArrayObjects<E> implements SomeImp<E> { int maxCapacity, actualSize; E[] array; // <- The missing declaration @SuppressWarnings(“unchecked”) // <- Suppress the “unchecked cast” warning public ArrayObjects() { maxCapacity = 10; array = (E[]) new Object[maxCapacity]; } } … Read more

[Solved] Java Pattern matcher and RegEx

If I am not mistaken, the strings all begin with G1:k6YxekrAP71LqRv. After that, there is [P:3] by itself, or with either left S:2,3,4|, right |R:2,3,4,5 or with both left and right. The values 2,3,4 and 2,3,4,5 could be repetitive digits divided by a comma. To match the full pattern you could use: (G1:k6YxekrAP71LqRv)\[(?:S:(?:\d,)+\d\|)?(P:3)(?:\|R:(?:\d,)+\d)?\] Explanation (G1:k6YxekrAP71LqRv) … Read more

[Solved] Printing 2D Matrix in a given format

The problem could be solved using recursion. For example, the code below prints exactly the required matrix for a given n. import java.util.Scanner; public class Main { public static void main(final String[] args) { final Scanner scanner = new Scanner(System.in); final int n = scanner.nextInt(); final int[][] matrix = create(1, (int) Math.pow(2, n)); print(matrix); } … Read more

[Solved] How to get information from an object? [duplicate]

You have a couple of options, but the generally best approach here is to create getters: public class PersonalId { // ——————- renamed: it was “personal_id” private int id = 0; // ——————- fixed: was “privete” instead of “private” private String name; public PersonalId(String name) { // ——————- renamed: it was “personal_id” this.name = name; … Read more

[Solved] Trying to test if an input is odd or even

You’d better simplify all of this, you don’t need to call your method multiple times : public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (writeAndCheckEven(Integer.parseInt(scanner.nextLine()))) { System.out.println(“You added an even number, go on”); } System.out.println(“You added an odd number, done!”); } private static boolean writeAndCheckEven(int number) { return number % 2 … Read more

[Solved] Why iterators are not a solution for CuncurentModificationException?

From https://docs.oracle.com/javase/7/docs/api/java/util/ConcurrentModificationException.html For example, it is not generally permissible for one thread to modify a Collection while another thread is iterating over it. In general, the results of the iteration are undefined under these circumstances. Some Iterator implementations (including those of all the general purpose collection implementations provided by the JRE) may choose to throw … Read more

[Solved] Saving a web page to a file in Java [closed]

This is HTTP. You can’t just open a socket and start reading something. You have to be polite to the server and send a request first: socket.getOutputStream().write(“GET /index.html HTTP/1.0\n\n”.getBytes()); socket.getOutputStream().flush(); Then read a HTTP response, parse it, and get your html page back. EDIT I wrote what to do with sockets only because it was … Read more

[Solved] How to create following JSON using JSONObject java?

Your question is unclear but if you just want an example of JSONObject then the code below can generate what you want. JSONObject car = new JSONObject(); car.put(“car”, new JSONObject()); JSONArray brands = new JSONArray(); brands.put(“C”); brands.put(“D”); car.put(“brands”, brands); JSONArray cars = new JSONArray(); cars.put(car); JSONObject json = new JSONObject(); json.put(“cars”, cars); System.out.println(json.toString(2)); The output … Read more

[Solved] how to get strings from a string array and show it one by one in a TexttView using Buttons (android)

The function for onClick attribute in XML has to be something like, public void yourFunction (View view) { //Your code } Since you didn’t add View as the argument of your function, your layout is never going to get to it, hence when you click on your button, the app crashes. You also need to … Read more