I have to create a method that will remove at certain value from an array and create a new array without that certain value. For example, if my array is (0,2,3,5,3) and I want to remove 3, the new array should be (0,2,5). For some reason, it only works for the first two digits.
import java.util.Scanner;
public class removeDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
//array of numbers
int array[] = new int[] {0,1,2,3,4,5};
//invokes method and prints result
//System.out.println(remove(3,array));
remove(3,array);
}
//method remove that removes selected number from array
public static int[] remove(int v, int[] in) {
//count variable counts how many non-target numbers
int count = 0;
//for loop that checks if value at certain index is not equal to "v", the target number for removal
for(int k = 0; k < in.length; k++) {
//checks if certain number at certain index of array is not equal to v, or in this case, 3
if(in[k] != v) {
//counter
count++;
}
}
//new array that will stores values except "v"
int copy[] = new int[count];
//prints the length
System.out.println("array length: " + copy.length);
//for loop that checks if number not 3
for(int a = 1; a < in.length;) {
// sets number at certain index of main array into new array
if(in[a] != 3){
copy[a] = in[a];
a++;
System.out.println(copy[0]);
System.out.println(copy[1]);
System.out.println(copy[2]);
System.out.println(copy[3]);
}
else if(in[a] == 3) {
copy[a] = in[a+1];
}
}
//returns new array
return copy;
}
}
As said before, I need the new array to exclude the targeted number for removal.
You need two index variables for making the copy: one runs through the input array (a, as in the original code), and the other tracks your position in the output array (b, new variable). They can not be calculated from each other (they are the same at the beginning, but b can be significantly less than a at the end)
int b = 0;
for(int a = 0; a < in.length; a++) {
if(in[a] != v) {
copy[b] = in[a];
b++;
}
}
Using Java8 and it's streams feature you could do something like :
public static void main(String[] args) {
int[] array = {3236,47,34,34,73,46,3,64,473,4,4,346,4,63,644,4,6,4};
int[] newArray = removeAllOccurencesOf(array, 4);
System.out.println(Arrays.toString(newArray));
}
public static int[] removeAllOccurencesOf(int[] array, int numberToRemove)
{
//stream integers from array, filter the ones that correspond to number to remove, get what's left to new array
int[] newArray = IntStream.of(array).filter(i->i!=numberToRemove).toArray();
return newArray;
}
You can achieve the same result with some code like this:
// Add to params all inputs to remove from array
List<Integer> params = new ArrayList<>();
// Use Integer class instead of int datatype
Integer array[] = new Integer[] {0,1,2,3,4,3};
// Convert array to List class
List<Integer> list = new ArrayList<>(Arrays.asList(array));
// Remove all matches
list.removeAll(params);
Related
very new to java and oop in general. so be kind.
I have one text file which contains 10 integers, searchkeysArray.txt. The program creates an array named keysArr. I have another text file of 500 random integers, array1.txt. The program creates another array named array1.
I want to use the linearSearch method I created to search for the elements of keysArr within array1 and output the index which it exist.
public static int linearSearch(int arr[], int x)
{
int size = arr.length;
for(int i = 0; i < size; i++)
{
if(arr[i] == x)
return i;
}
return -1;
}
readFile method
public static int[] readFile(String file)
{
try {
File f = new File(file);
Scanner s = new Scanner(f);
int ctr = 0;
while (s.hasNextInt())
{
ctr++;
s.nextInt();
}
int[] arr = new int[ctr]; //create array of that size
Scanner scanner2 = new Scanner(f);
for (int i = 0; i < arr.length; i++)
arr[i] = scanner2.nextInt();
return arr;
}
catch(Exception e)
{
return null;
}
the program.
public static void main(String[] args)
{
int[] keysArr = readFile("searchkeysArray");
int[] array1 = readFile("array17");
int key = 34;
int result = linearSearch(array1, key);
if (result != -1)
System.out.print("The element " +key+" is present in the array, at index " + result + " ");
else
System.out.print("The element " +key+" is not present in the array ");
}
and it outputs
The element 34 is present in the array, at index 359
which makes sense. I've manually tested numbers and (apparently) everything works fine. But I do not quite understand how I'm supposed to use keysArr as my key rather than int x = some number.
Want to output something like
The element [keysArr[0]] is present in the array, at index 359
The element [keysArr[1]] is present in the array, at index 547
...
The element [keysArr[4]] is not present in the array
and so on.
Right now keysArr just an array of 10 integers but I will eventually use hundreds..
Rather than using a specific hard-coded key, such as int key = 34, you wish to loop over your array of keys keysArr. You can achieve that by using code like:
for (int key : keysArr) {
int result = linearSearch(array1, key);
if (result != -1)
System.out.print("The element " +key+" is present in the array, at index " + result + " ");
else
System.out.print("The element " +key+" is not present in the array ");
}
Your linear algorithm worth O(n^2) which is pretty bad if you are going to increase the number of integers.
I suggest you to load dictionary elements into a hash-map Value -> Index and iterate through the values array looking up for the position in the hashMap. This will give you O(2n) - the complexity will be linear. You'll require 2n of memory though. But in your case it is insignificant.
public static void main(String[] args) {
printResults(new int[]{1, 2, 3, 4}, new int[]{3, 4, 7, 9});
}
public static void printResults(int dictionary[], int valuesToSearch[]){
Map<Integer, Integer> positionsMap = new HashMap<>();
IntStream.range(0, dictionary.length).forEach(index -> positionsMap.put(dictionary[index], index));
Arrays.stream(valuesToSearch).mapToObj(value -> positionsMap.containsKey(value) ?
format("Value %s is present in the array at index %s", value, positionsMap.get(value)) : format("Value %s is not present in the array", value)
).forEach(System.out::println);
}
I have an array of unknown length in java.
I want to remove 1 percent of the total elements of the array after sorting it.
How can I do that? If I pass an array and a length to a function.
public double deleteElements(double array[], int length) {
int trimmedLength = array.length-length;
for (int i = 0; i < trimmedLength; i++) {
}
}
I assume you mean something like :
import java.util.Arrays;
public class Test{
public static void main(String[] args){
double[] testArray = new double[]{0,1,2,3,4,5,6,7,8,9};
//implement sort, if needed
System.out.println(Arrays.toString( deleteElements(testArray, 5) ));
}
public static double[] deleteElements(double array[],int newLength){
//add check array != null
//add check length is <= array.length
double[] returnArray = new double[newLength];
for(int i=0;i<newLength;i++){
returnArray[i] = array[i];
}
return returnArray;
}
}
I made an assumption that the length you are passing in is the percent of elements you want to remove from the list.
This converts your array to a list, sorts it, and converts the remaining percentage back to an array which is returned.
public double[] deleteElements(double[] doubles, int length)
{
List<Double> elements = Arrays.stream(doubles).boxed()
.collect(Collectors.toList());
Collections.sort(elements);
int startElement = (int) (elements.size() / 100.0 * length);
List<Double> subList = elements.subList(startElement, elements.size());
return subList.stream().mapToDouble(d -> d).toArray();
}
I'm trying to find out particular elements in my ArrayList (arrayOne). Each element should be an int[]. I've tried System.out.println(arrayOne), which compiles but gives a irregular and strange number "[[I#370968]".
I've also tried System.out.println(arrayOne[0]) but it won't compile and emits the error
Array required but java.util.ArrayList found.
Given is the following code, with {1,12,3,13,123,2} passed to eg:
import java.util.ArrayList;
public class arrayTest {
private ArrayList<int[]> arrayOne;
public arrayTest(int[] eg) {
int[] xy = new int[2];
arrayOne = new ArrayList<int[]>(eg.length);
for (int i = 0; i < eg.length; i++) {
int sv = String.valueOf(eg[i]).length();
if (sv == 1) {
xy[0] = 0;
xy[1] = eg[i];
arrayOne.add(xy);
}
else if (sv == 2) {
System.out.println("two digits");
// TODO add code to make xy[0] = the first
// digit of eg and xy[1] = the second digit
}
else {
System.out.println("too many digits");
// and throw error accordingly
}
System.out.println(arrayOne);
}
}
}
How do make sure and print out the int array at arrayOne[0]
Given the code above if (sv == 2) and i want to split each individual number into an int[] with [0] being the first digit and [1] being the second digit how would i get the int value of each individual digit.
Use Arrays.toString(yourArray); to print out arrays in human readable form.
First of all, the string [I#370968 is displayed because you are trying to print an int[], which is actually an object. Because this object does not override the object's toString() method, that method is derived from the Object class. The Object.toString() implementation, which prints the class name (in this case [I, because it is an int array), then an # sign, and then the hash code of the object.
Your ArrayList contains a number of int[]s. Because an ArrayList is not an array (the one with the square brackets, like int[]), you can't call an element on it as if it were an array. In short, you cannot call arrayOne[someDesiredIndex].
In order to get an element from the ArrayList, call get(int index) on it; it returns the desired int[]. As already pointed out by another answer, you can use Arrays.toString(int[]) to print it in a human readable form.
To answer your questions:
You can retrieve the first index (0) of the first array inside arrayOne with the following code: arrayOne.get(0)[0].
The following code should work:
private static int[] intToArray(int n) {
String str = String.valueOf(n);
int length = str.length();
int[] ints = new int[length];
for (int i = 0; i < length; i++) {
ints[i] = Integer.parseInt(str.substring(i, i + 1));
}
return ints;
}
Above method puts each digit into the next array position (it also works with digits greater than 99). With this method you can easily get each individual digit:
int[] digits = intToArray(47);
int a = digits[0]; // Will be 4
int b = digits[1]; // Will be 7
So this is the class rewritten:
public class Rewrite {
private ArrayList<int[]> arrayOne = new ArrayList<int[]>();
public Rewrite(int[] eg) {
for (int i = 0; i < eg.length; i++) {
int length = String.valueOf(eg[i]).length();
switch (length) {
case 1:
this.arrayOne.add(new int[] { 0, eg[i] });
break;
case 2:
this.arrayOne.add(intToArray(eg[i]));
break;
default:
throw new IllegalArgumentException("Number " + eg[i] + " has too many digits");
// Or display the error or something.
}
System.out.println(Arrays.toString(this.arrayOne.get(i)));
}
}
private static int[] intToArray(int n) {
String str = String.valueOf(n);
int length = str.length();
int[] ints = new int[length];
for (int i = 0; i < length; i++) {
ints[i] = Integer.parseInt(str.substring(i, i + 1));
}
return ints;
}
public static void main(String[] args) {
Rewrite r = new Rewrite(new int[] { 47, 53, 91, 8 });
}
I need a shuffle method to shuffle elements of an array which holds objects from another class. At the moment I wrote this code to test with integers first, but it seems to not working perfectly. Most of the elements are being duplicated.
Can someone please spot the mistake?
And also come up with a more efficient method for this.
I am not sure if I can use collections.shuffle because I have further use of my shuffled array later.
public static void shuffle()
{
int[] a = new int[52];
int count = 0;
int random = 0;
while (count!=51){
random = (int)(Math.random() * 52);
for (int i=0; i <=count; i++){
if (a[count] != b[random])
result = true;
}
if (result){
a[count] = b[random];
count++;
}
b = Arrays.copyOf(a, a.length);
}
}
First you should not define shuffle() in this way. I would treat b as a parameter and pass it into shuffle() instead of a static field (as your shuffle() is declared as static, your b is also static right? It looks strange to share b between all instances), and result is declared as a local variable.
This part
for (int i=0; i <=count; i++){
if (a[count] != b[random])
result = true;
}
checks whether any one of a[0], a[1] until a[count] is not equal to b[random]. If yes, then assign b[random] to a[count] and increase count by 1. As a[] is not initialized, it is only an array of 0. (a[count] != b[random]) appears to be always true and hence result is true.
Then, for this part,
if (result){
a[count] = b[random];
count++;
}
say for example random=5, then at the first round of the while loop a[0]=b[5], count=1 (due to count++), and b becomes an array of b[5] and a series of 0. (Due to
b = Arrays.copyOf(a, a.length);
all other elements are replaced by 0.)
Edit: Here I provide a simple method, not thoroughly tested, but should work:
public static int[] shuffle(int[] array) {
int[] a = new int[array.length];
//convert int[] to ArrayList<Integer>
ArrayList<Integer> list = new ArrayList<>();
for (int i: array)
list.add(i);
//now shuffle:
for (int i=0; i<array.length; i++) {
int rand = (int)(Math.random()*list.size());
a[i] = list.remove(rand);
}
return a;
}
The array returned is shuffled. Actually I can't say the method "shuffles" the array. It simply creates an empty array, and repeatedly selects an element randomly and put it at the front.
Edit2: This really "shuffles", and this is another approach: does not return a new array. It shuffles the array, 100 times.
public static void shuffle(int[] array) {
for (int i=0; i<100; i++) {
int r1 = (int)(Math.random()*array.length);
int r2 = (int)(Math.random()*array.length);
int tmp = array[r1];
array[r1] = array[r2];
array[r2] = tmp;
}
}
import java.util.Random;
public class Shuffle {
public static void main(String[] args) {
Integer[] a = new Integer[52];
for (Integer i=0;i<a.length;i++) a[i] = i+1;
// Let's shuffle
Random rd = new Random();
for (Integer i=0;i<a.length;i++){
Integer changeBy = rd.nextInt(a.length);
Integer aux=a[i];
a[i]=a[changeBy];
a[changeBy] = aux;
}
// Now show the shuffled array
for (Integer i=0;i < a.length; i++) System.out.print(a[i]+",");
}
}
Hope this small algorithm helps you. As you can see from 2 different runs, it really shuffles your array:
11,1,24,13,28,15,25,48,5,22,12,32,29,42,34,7,33,31,47,18,51,40,8,17,41,20,6,36,21,45,27,52,38,10,30,14,23,19,43,4,50,46,44,3,49,37,35,2,9,26,16,39
3,10,37,26,41,15,28,52,6,24,20,43,33,21,51,32,25,40,50,8,7,5,4,35,13,16,49,17,29,47,12,14,36,39,45,30,2,42,23,38,31,19,27,46,34,11,18,1,22,48,9,44
Why no you HashSet for shuffle?
Elements in java.lang.HashSet are shuffling by their hashcode.
public static void shuffle()
{
int [] b; // you origin array
Set<Integer> temp = new HashSet<Integer>();
for (int i : b) {
temp.add(i);
}
Integer [] a = new Integer[b.length];
a = temp.toArray(a); // new shuffle array
}
I know most people don't like writing methods for people but i was hoping someone could help me convert my algorithm into Java code. I hope my algorithm is good and actually works.
Sort a given array of ints into ascending order. Set Group Limit to 15 (that means that the sum of the group is not greater than 15).
Take the first element of the sorted array and insert into a Group (new array/list) eg. Group A.
Take the second element of the sorted array and insert unless it will make it exceed the group limit. If it exceeds, create a new Group B and insert there.
Take third element and try to insert into next available group.
Repeat until all ints have been checked and grouped.
Input:
egArray = [1,3,4,6,6,9,12,14]
Output:
Group A: [1,3,4,6], Group B: [6,9], Group C: [12], Group D: [14]
I have tried to do this, but failed epically, not even worth me posting my code. :-(
This is an example data and an algorithm I've made up for self learning, so please keep the criticism to a minimum. I genuinely learn from a lot of Stackoverflow posts people have written over the last few months, unfortunately I couldn't find one like this example. Thanks.
Try this:
public static void main(String[] arguments) {
int limit = 15;
int[] egArray = new int[] { 14, 1, 3, 4, 6, 6, 9, 12 };
ArrayList<ArrayList<Integer>> a = grouping(limit, egArray);
System.out.println(a);
}
public static ArrayList<ArrayList<Integer>> grouping(int limit, int[] array) {
// Sort the input array.
Arrays.sort(array);
// Copy the int[] to an ArrayList<Integer>
ArrayList<Integer> input = new ArrayList<>();
for (int i = 0; i < array.length; i++) {
input.add(array[i]);
}
// Initialize the groups
ArrayList<ArrayList<Integer>> groups = new ArrayList<>();
groups.add(new ArrayList<Integer>());
// Initialize the sums of the groups, to increase performance (I guess).
ArrayList<Integer> sums = new ArrayList<>();
sums.add(0);
// Iterate through the input array until there is no number
// left in it (that means we just added all the numbers
// into our groups array).
while (!input.isEmpty()) {
int n = input.get(0); // Store the number to 'n', to shortcut.
if (n > limit) {
String msg = "number is greater than the limit; cannot add number";
throw new IllegalArgumentException(msg);
// Or whatever to do if the number is larger than the limit.
}
boolean match = false;
// Search the next groups and check if our current
// number ('n') fits.
for (int i = 0; i < sums.size(); i++) {
if (sums.get(i) + n <= limit) {
// If it fits, then add the number to the group.
sums.set(i, sums.get(i) + n);
groups.get(i).add(n);
match = true;
break;
}
}
// If 'n' doesn't fit in any group, create a new one.
if (!match) {
ArrayList<Integer> e = new ArrayList<>();
e.add(n);
groups.add(e);
sums.add(n);
}
// Remove our number.
input.remove(0);
}
return groups;
}
Notice that the method returns an ArrayList<ArrayList<Integer>> instead of an int[][], but the effect is the same. In order to check the values of the groups, just run the main(String).
How about this method?
public static ArrayList group(ArrayList<Integer> arr, Integer groupLimit) {
ArrayList<ArrayList> result = new ArrayList<ArrayList>();
ArrayList<Integer> temp = new ArrayList<Integer>();
for (Integer x : arr) {
if (sumElements(temp) + x < groupLimit) {
temp.add(x);
} else {
result.add(temp);
temp = new ArrayList<Integer>();
temp.add(x);
}
}
if (temp.size() > 0) {
result.add(temp);
}
return result;
}
public static int sumElements(ArrayList<Integer> arr) {
Integer result = 0;
for(Integer x:arr) result += x;
return result;
}