I'm trying to resize an Array[] using a method named resize(). I think I've coded the method correctly, however the original array does not resize after running it through the method, and I'm not sure what I'm doing wrong.
I've tested that the newArr[] is in fact twice the length of the oldArr[] and contained the values of the oldArr[], but I can't figure out why it's not taking affect to the wordList[] in the main method.
My assignment is to use Arrays and not Arraylists, so using that is out of the question.
infile = new BufferedReader( new FileReader(infileName) );
while (infile.ready()) {
String s = infile.readLine();
System.out.println(wordList.length);
System.out.println(count);
wordList[count] = s;
if(s.length() > lenOfLongest)
lenOfLongest = s.length();
if(s.length() < lenOfShortest)
lenOfShortest = s.length();
count++;
if(count == wordList.length)
resize(wordList);
}
private static String[] resize( String[] oldArr ) {
String[] newArr = new String[oldArr.length * 2];
System.out.println(newArr.length);
for(int i = 0;i < oldArr.length; i++) {
newArr[i] = oldArr[i];
}
return newArr;
}
if(count == wordList.length)
resize(wordList);
should be:
if(count == wordList.length)
wordList = resize(wordList);
See this answer for more details.
As #pad says in his comment: you don't reassing to wordList.
And your resize() method returns a new array. You should therefore reassing wordList.
As a general note, arrays are not resizable in Java.
Because you are not assign the returned array to anything.Must be as follows.
String[] temp = resize(wordList);
Related
I have this method which is supposed to cut a String when it finds a symbos of + or - (it is part of a derivative calculator, so the idea is to get a bunch of small strings separated by + or - and then derivate them one by one. That's why i have that search for opened and closed parenthesis)
The problem is: When calling res = new String(); it will create a new String, but it will also set all the existing String objects in the array to null, which means the return of the method will always be an array with everything set to null (unless it does not find a + or - in the function).
Is there a way around this?
public static String[] cutString(String func)
{
int number_strings_res = 0;
int number_parenthesis = 0;
String[] res = new String[1];
res[0] = new String(func);
for(int i = 0; i < func.length(); i++)
{
if(func.charAt(i) == '+' || func.charAt(i) == '-')
{
for(int j = 0; j < i; j++)
{
if(func.charAt(j) == '(')
number_parenthesis++;
if(func.charAt(j) == ')')
number_parenthesis--;
}
if(number_parenthesis == 0)
{
res[number_strings_res] = "";
for(int j = 0; j < i; j++)
{
res[number_strings_res] += func.charAt(j);
}
number_strings_res++;
res = new String[number_strings_res + 1];
res[number_strings_res] = new String(Character.toString(func.charAt(i)));
number_strings_res++;
res = new String[number_strings_res + 1];
res[number_strings_res] = new String();
}
}
}
return res;
}
res = new String[number_strings_res + 1] allocates a new array and stores it to res. So that's the source of the immediate problem. You should probably be using a List, like ArrayList, instead if you don't know how long the collection will be.
When calling res = new String() ...
You have no such call, and it would not compile anyway.
'res' is declared as variable referring to a String array. When you execute res = new String[...] you naturally replace the entire array of Strings.
But I can't really figure out what you intend? Are you trying to extend the array? You can't, directly.
If you must use an array, look into Arrays.copyFrom. But an ArrayList would be easier.
I've scoured a couple of the SOF threads but can't seem to find the answer I'm looking for. Most of them provide an answer with code that's beyond the scope of what I have learned thus far.
I've tried quite a few different things and can't get this to work the way I need it to.
The program is supposed to take the given array, read it, find the given toRemove item, and re-print the array without the toRemove item.
I believe my issue is within the removeFromArray method
public static void main(String[] args)
{
String[] test = {"this", "is", "the", "example", "of", "the", "call"};
String[] result = removeFromArray(test, "the");
System.out.println(Arrays.toString(result));
}
public static String[] removeFromArray(String[] arr, String toRemove)
{
int newLength = 0;
for(int i = 0; i < arr.length; i++)
{
if(arr[i].contains(toRemove))
{
newLength++;
}
}
String[] result = new String[arr.length-newLength];
for(int i = 0; i < (result.length); i++)
{
if(arr[i].contains(toRemove))
{
}
else
{
result[i] = arr[i];
}
}
return result;
}
This is an assignment in my java class and we have not learned Lists (one of the answers I stumbled upon in my googling) yet so that is not an option for me.
As it is now, it should be outputting:
[this, is, example, of, call]
Currently it is outputting: [this, is, null, example, of]
Any and all help will be much appreciated!
You need 2 indices in the second loop, since you are iterating over two arrays (the input array and the output array) having different lengths.
Besides, newLength is a confusing name, since it doesn't contain the new length. It contains the difference between the input array length and the output array length. You can change its value to match its name.
int newLength = arr.length;
for(int i = 0; i < arr.length; i++)
{
if(arr[i].contains(toRemove))
{
newLength--;
}
}
String[] result = new String[newLength];
int count = 0; // count tracks the current index of the output array
for(int i = 0; i < arr.length; i++) // i tracks the current index of the input array
{
if(!arr[i].contains(toRemove)) {
result[count] = arr[i];
count++;
}
}
return result;
There's the error that #Eran pointed out in your code, which can solve your problem. But I'm going to discuss another approach.
For now, you're first iterating over the entire array to find the number of occurrences to remove, and then, you're iterating over the array to remove them. Why don't you just iterate over the array, just to remove them. (I know, your first loop is helping you to determine the size of the output array, but you don't need that if you use some List like ArrayList etc.)
List<String> resultList = new ArrayList<String>();
for(int i = 0; i < arr.length; i++)
{
if(!arr[i].contains(toRemove))
{
resultList.add(arr[i]);
}
}
And you can return the resultList, but if you really need to return an array, you can convert the resultList to an array like this:
String [] resultArray = resultList.toArray(new String[resultList.size()]);
And then return this array. See this approach live here on ideone.
Try this Java8 version
List<String> test = Arrays.asList("this", "is", "the", "example", "of", "the", "call");
test.stream()
.filter(string -> !string.equals("the"))
.collect(Collectors.toList())
.forEach(System.out::println);
You can use Java Stream instead, it will give you the expected result and also your code will be clearer and really smaller.
See the method below I wrote that solves your problem.
public static String[] removeFromArray(String[] arr, String toRemove) {
return Arrays.stream(arr)
.filter(obj -> !obj.equals(toRemove))
.toArray(String[]::new);
}
If you're unfamiliar with java Stream, please see the doc here
The following code removes all occurrences of the provided string.
Note that I have added few lines for validate the input, because if we pass a null array to your program, it would fail. You should always validate the input in the code.
public static String[] removeFromArray(String[] arr, String toRemove) {
// It is important to validate the input
if (arr == null) {
throw new IllegalArgumentException("Invalid input ! Please try again.");
}
// Count the occurrences of toRemove string.
// Use Objects.equals in case array elements or toRemove is null.
int counter = 0;
for (int i = 0; i < arr.length; i++) {
if (Objects.equals(arr[i], toRemove)) {
counter++;
}
}
// We don't need any extra space in the new array
String[] result = new String[arr.length - counter];
int resultIndex = 0;
for (int i = 0; i < arr.length; i++) {
if (!Objects.equals(arr[i], toRemove)) {
result[resultIndex] = arr[i];
resultIndex++;
}
}
return result;
}
So, I am writing my program when suddenly, I run into a problem in which my three arrays print as null, 0 and null.
The program reads values from a file, and assigns them to the three arrays based on the iteration number.
Here is my code:
String mushroom []= new String [10];
int weight [] = new int [10];
String cooking[] = new String [10];
FileReader fr = new FileReader ("7J_Mushrooms.csv");
BufferedReader br = new BufferedReader (fr);
System.out.println("Mushroom\t\tWeight\t\tCooking\n=======================================================");
String line = "";
while ((line = br.readLine()) != null){
String [] temp = line.split(",");
for (int i = 0; i < temp.length; i++){
if(i == 0){
mushroom[i] = temp[i];
}
else if (i == 1){
weight[i] = Integer.parseInt(temp[i]);
}
else{
cooking[i] = temp[i];
}
}
}
// This bit just sorts them by weight in ascending order like a parallel array
for (int i = 0; i < weight.length-1; i++){
for (int j = 0; j < weight.length-1;j++){
if (weight[j] > weight[j+1]){
int temp = weight [j];
String tempMush = mushroom [j];
String tempCook = cooking [j];
weight[j] = weight[j+1];
mushroom[j] = mushroom[j+1];
cooking[j] = cooking[j+1];
weight[j+1] = temp;
mushroom[j+1] = tempMush;
cooking[j+1] = tempCook;
}
}
}
for (int i = 0; i < weight.length; i++){
System.out.print(mushroom[i] + "\t\t" + weight[i] + "\t\t" + cooking[i] + "\n");
}
When I print the values of the arrays inside of the for loop, the values are correct, however outside of the while loop the code is printed as null, null and 0. However, the last three values are printed but I am sure that is something to do with my problem.
Anyway, I believe it is to do with scope.
After some searching, I discovered that java is "Pass by Value" instead of "Pass by Reference". I do not really understand this principal, but to my understanding it affects methods in particular, but all of my code is under a single method -- main. I tried to use return inside the for loop and outside but it does not work either!
The way you are initially reading the values in seems quite off: you are placing the corresponding weight and cooking into different indices than the actual mushroom and you are using the index i in completely the wrong way. It should probably be
int i = 0;
while ((line = br.readLine()) != null){
String[] temp = line.split(",");
if(temp.length == 3)
mushroom[i] = temp[0];
weight[i] = Integer.parseInt(temp[1]);
cooking[i] = temp[2];
} else {
// some error handling
}
i++;
}
"When I print the values of the arrays inside of the for loop, the values are correct" is wrong - if you inspect temp the values are correct but mushroom, weight and cooking are never being filled correctly with your code.
Further note:
try using a custom class to hold the associated values instead of dealing with 3 arrays which magically have something to do with each other. Then sort an array of instances of that class
You are writing/over-writing only first three elements in each mushroom, cooking and weight.
As
i < temp.length, i <= 3
I am trying to remove duplicated words from an array, and I keep getting null values. I'm not allowed to use java sorting methods so I have to develop my own. Here's my code:
public class Duplicate{
public static void main(String[] args){
String[] test = {"a", "b", "abvc", "abccc", "a", "bbc", "ccc", "abc", "bbc"};
removeDuplicate(test);
}
public static String[] removeDuplicate(String[] words){
boolean [] isDuplicate = new boolean[words.length];
int i,j;
String[] tmp = new String[words.length];
for (i = 0; i < words.length ; i++){
if (isDuplicate[i])
continue;
for(j = 0; j < words.length ; j++){
if (words[i].equals(words[j])) {
isDuplicate[j] = true;
tmp[i] = words[i];
}
}
}
for(i=0;i<words.length;i++)
System.out.println(tmp[i]);
return tmp;
}
}
I tried doing
if(words == null)
words == "";
But it doesn't work. I also want to return the tmp array with a new size.
For example, test array length = 9, after removing the duplicates,I should get a new array with a length of 7.Thank you for your help.
EDIT:
result i get:
a
b
abvc
abccc
null
bbc
ccc
abc
null
You're getting nulls because the result array contains fewer words than the input array. However, you're constructing the arrays of the same length.
You don't have to sort to solve this problem. However, if you're not allowed to use the tools provided by java.utils, then this is either a poorly contrived test question or whomever told you not to use the Java utility classes is poorly informed.
You can solve without sorting by doing (assuming Java 1.5+):
public class Duplicate {
public static void main(String[] args) {
String[] test = {"a", "b", "abvc", "abccc", "a", "bbc", "ccc", "abc", "bbc"};
String[] deduped = removeDuplicate(test);
print(deduped);
}
public static String[] removeDuplicate(String[] words) {
Set<String> wordSet = new LinkedHashSet<String>();
for (String word : words) {
wordSet.add(word);
}
return wordSet.toArray(new String[wordSet.size()]);
}
public static void print(String[] words) {
for (String word : words) {
System.out.println(word);
}
}
}
The output will be:
a
b
abvc
abccc
bbc
ccc
abc
I would go for hashset to remove duplicates, it will remove duplicates since hash function for the same string will give same value, and duplicates will be eliminated. Then you can convert it to a string.
I would recommend doing this with a different approach. If you can use an ArrayList, why not just create one of those, and add the non-duplicate values to it, like this:
ArrayList<String> uniqueArrayList = new ArrayList<String>();
for(int i = 0; i < words.length; i++){
if(!uniqueArrayList.contains(words[i])){ // If the value isn't in the list already
uniqueArrayList.add(words[i]);
}
}
Now, you have an array list of all of your values without the duplicates. If you need to, you can work on converting that back to a regular array.
EDIT
I really think you should use the above option if you can, as there is no clean or decently efficient way to do this only using arrays. However, if you must, you can do something like this:
You can use the code you have to mark values as null if they are duplicates, and also create a counter to see how many unique values you have, like this:
int uniqueCounter = 0;
for(int i = 0; i < isDuplicate.length; i++){
if(!isDuplicate[i]){
uniqueCounter++;
}
}
Then, you can create a new array of the size of unique items, and loop through the words and add non-duplicate values.
String[] uniqueArray = new String[uniqueCounter];
int uniqueIndex = 0;
int wordsIndex = 0;
while(index < uniqueArray.length){
// Check if words index is not a duplicate
if(!isDuplicate[wordsIndex]){
// Add to array
uniqueArray[uniqueIndex] = words[wordsIndex];
uniqueIndex++; // Need to move to next spot in unique.
}
// Need to move to next spot in words
wordsIndex++;
}
Again, I HIGHLY recommend against something like this. It is very poor, and pains me to write, but for the sake of example on how it could be done using an array, you can try it.
I don't have the time to write functioning code, but I would reccomend to first sort the array using Arrays.sort(stringArray) and then loop throug the array coparing one string to the previous. Strings that match the previous one are duplicates.
Note: This method is probably not the fastest one and though only should be used on small arrays or in tasks where performance does not matter.
What about this approach?
public static String[] removeDuplicate(String[] words){
// remember which word is a duplicate
boolean[] isDuplicate = new boolean[words.length];
// and count them
int countDuplicate = 0;
for (int i = 0; i < words.length ; i++){
// only check "forward" because "backwards checked" duplicates have been marked yet
for(int j = i + 1; j < words.length ; j++){
if (words[i].equals(words[j])) {
isDuplicate[j] = true;
countDuplicate++;
}
}
}
// collect non-duplicate strings
String[] tmp = new String[words.length - countDuplicate];
int j = 0;
for (int i = 0; i < isDuplicate.length; i++) {
if (isDuplicate[i] == false) {
tmp[j] = words[i];
j++;
}
}
// and return them
return tmp;
}
I need read data from csv file and much more convinience for me is put there to 2D array (to my mind it's easiest way to work with this "schedule" data).
Each file line contained information in following format:
Instructor, Course, Group, Student, Result
as follows example:
Paul Schwartz,Introduction to Computer Architecture,I1,Ben Dunkin,88
Muhamed Olji,Object Oriented Programming,I4,Mike Brown,73
But my code needs some simplify. But I don't know how to make it easier and ask of You.
Code:
private String[][] fileContent(String pathToCSVFile) {
final int ROWS = 100;
final int COLUMNS = 5;
String fileData[][] = new String[ROWS][COLUMNS];
Scanner scanner = new Scanner(pathToCSVFile);
boolean done = false;
int i, j;
while (!done) {
for (i = 0; i >= 0; i++) {
for (j = 0; j >= 0; j++) {
String str[] = scanner.nextLine().split(",");
for (int element = 0; element < str.length; element++) {
fileData[i][element] = str[element];
if (i >= ROWS) {
Arrays.copyOf(fileData, fileData.length * 2);
}
}
}
}
if (!scanner.hasNextLine()) done = true;
}
return fileData;
}
How to refactor this snippet of code for better simplicity?
Does exist any better way for partially filled array (than Arrays.copyOf(fileData, fileData.length * 2))?
Using openCSV, you can get a list containing all the lines and convert it to an array (or just keep the list):
try (CSVReader reader = new CSVReader(new BufferedReader(
new FileReader(pathToCSVFile)));) {
List<String[]> lines = reader.readAll();
return lines.toArray(new String[lines.size()][]);
}
(using Java 7 try-with-resources syntax)
First of all, be careful with those for loops. They are "almost" undefined loops, because they start with i,j=0, and loop while >=0 (always, until they overflow into a negative number).
And why do you need them anyway? I think with you while and the for(element) you are done, right?
Something like that (I didn't tried, is just to explain the concept)
private String[][] fileContent(String pathToCSVFile) {
final int ROWS = 100;
final int COLUMNS = 5;
String fileData[][] = new String[ROWS][COLUMNS];
Scanner scanner = new Scanner(pathToCSVFile);
boolean done = false;
int i=0;
while (!done) {
String str[] = scanner.nextLine().split(",");
for (int element = 0; element < str.length; element++) {
fileData[i][element] = str[element];
if (i >= ROWS) {
Arrays.copyOf(fileData, fileData.length * 2);
}
}
if (!scanner.hasNextLine())
done = true;
else
i++;
}
return fileData;
}
By the way, why don't you use objects, like an ArrayList? It would make your life easier, so you don't have to worry about memory handling. You just add new objects.
Something like an ArrayList <ArrayList <String>>